我是八度新手。我有两个矩阵。我必须将一个矩阵的特定列与另一个矩阵进行比较(我的矩阵 A 包含超过 5 个变量,类似地,矩阵 B 包含相同的变量。)并且如果矩阵 A 的第一列中的元素等于第二列中的元素矩阵 B 然后我必须使用第二个矩阵 B 的第三列来计算某些值。我通过使用 for 循环使用八度音程来执行此操作,但它会消耗大量时间来进行一天的计算,我必须这样做一年了。因为矩阵的大小非常大。请建议一些替代方法,以便我可以减少时间和计算量。
先感谢您。
感谢您的快速回复-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]
这里 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 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 . 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,
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:
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.
发布评论
评论(1)
您应该尽可能使用完整的矩阵或向量。您应该在交互式 shell 中尝试命令并检查中间结果,以了解它们如何组合在一起。
选择矩阵的第一列。您可以比较矩阵/向量,结果再次为 0/1 的矩阵/向量:
如果您分配结果,则可以再次使用它来索引到矩阵中:
这将选择
A
和B
的第一列相等的那些行的 >B。我希望这能让你开始。
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.
selects the first column of a matrix. You can compare matrices/vectors and the result is a matrix/vector of 0/1 again:
If you assign the result you can use it again to index into matrices:
This selects the third column of
B
of those rows where the first column ofA
andB
is equal.I hope this gets you started.