简体   繁体   English

用于循环性能的倍频程矩阵

[英]octave matrix for loop performance

I am new to Octave.我是八度的新手。 I have two matrices.我有两个矩阵。 I have to compare a particular column of a one matrix with the other(my matrix A is containing more than 5 variables, similarly matrix B is containing the same.) and if elements in column one of matrix A is equal to elements in the second matrix B then I have to use the third column of second matrix B to compute certain values.I am doing this with octave by using for loop, but it consumes a lot of time to do the computation for single day, i have to do this for a year.我必须将一个矩阵的特定列与另一个进行比较(我的矩阵 A 包含超过 5 个变量,同样矩阵 B 包含相同的变量。)如果矩阵 A 的第一列中的元素等于第二列中的元素矩阵 B 然后我必须使用第二个矩阵 B 的第三列来计算某些值。我通过使用 for 循环使用八度音阶来执行此操作,但它会消耗大量时间来进行单天的计算,我必须这样做一年。 Because size of matrices is very large.Please suggest some alternative way so that I can reduce my time and computation.因为矩阵的大小非常大。请提出一些替代方法,以便我可以减少时间和计算。 Thank you in advance.先感谢您。

Thanks for your quick response -hfs continuation of the same problem, Thank u, but this will work only if both elements in both the rows are equal.For example my matrices are like this,感谢您的快速响应-hfs 继续相同的问题,谢谢,但这只有在两行中的两个元素相等时才有效。例如我的矩阵是这样的,

A=[1 2 3;4 5 6;7 8 9;6 9 1]
B=[1 2 4; 4 2 6; 7 5 8;3 8 4]

here column 1 of first element of A is equal to column 1 of first element of B,even the second column hence I can take the third element of B, but for the second element of column 1 is equal in A and B,but second element of column 2 is different,here it should search for that element and print the element in the third column,and am doing this with for loop which is very slow because of larger dimension.In mine actual problem I have given for loop as written below:这里A的第一个元素的第1列等于B的第一个元素的第1列,即使是第二列,因此我可以取B的第三个元素,但是对于第1列的第二个元素在A和B中相等,但是第二个第 2 列的元素不同,这里它应该搜索该元素并在第三列中打印该元素,并使用 for 循环执行此操作,由于尺寸较大,该循环非常慢。在我的实际问题中,我已经给出了 for 循环以下:

for k=1:37651
    for j=1:26018
       if (s(k,1:2)==l(j,1:2))
          z=sin((90-s(k,3))*pi/180) , break ,end

      end
  end 

I want an alternative way to do this which should be faster than this.我想要一种替代方法来做到这一点,它应该比这更快。

You should work with complete matrices or vectors whenever possible.您应该尽可能使用完整的矩阵或向量。 You should try commands and inspect intermediate results in the interactive shell to see how they fit together.您应该在交互式 shell 中尝试命令并检查中间结果,以了解它们如何组合在一起。

A(:,1)

selects the first column of a matrix.选择矩阵的第一列。 You can compare matrices/vectors and the result is a matrix/vector of 0/1 again:您可以比较矩阵/向量,结果又是 0/1 的矩阵/向量:

> A(:,1) == B(:,1)
ans =

  1
  1
  0

If you assign the result you can use it again to index into matrices:如果您分配结果,您可以再次使用它来索引矩阵:

I = A(:,1) == B(:,1)
B(I, 3)

This selects the third column of B of those rows where the first column of A and B is equal.这将选择AB的第一列相等的那些行中B的第三列。

I hope this gets you started.我希望这能让你开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM