フィルターのクリア

How do I decrease the time taken by these two for loops. I am taking 30 seconds approximately to run this, and this is in a big for loop that runs for 10000 times.

3 ビュー (過去 30 日間)
Could anyone please help me with this? I am new to using MATLAB so I have written the code in a layman fashion and I am sure that there are ways to write it better.
The code is below (takes about 30 seconds, depending on size of both the matrices)
for i = 1:1:size(binary_probability_matrix,1)
for j = 1:1:size(matrix,1)
if matrix(j,1:2) == binary_probability_matrix(i,1:2)
matrix(j,3:4) = binary_probability_matrix(i,3:4);
elseif matrix(j,1) == binary_probability_matrix(i,2) && matrix(j,2) == binary_probability_matrix(i,1)
matrix(j,3:4) = binary_probability_matrix(i,3:4);
end
end
end
Basically I am comparing the first two columns of matrix and binary_probability_matrix. If the 1st 2 columns in the matrix are 4 3 ..then I search in the first two columns of binary_probability_matrix for 4 3 OR 3 4.
Similarly, when I am searching for 10 18, I search the first two columns of the binary_probability_matrix for finding 10 18 OR 18 10.
I hope that I was clear in mentioning my question. Please help me with this as the execution time is quite large.
EDIT: order of matrix is 5000x4 and the order of binary_probability_matrix is 2500x7,
Thank you
Jay
  2 件のコメント
Turlough Hughes
Turlough Hughes 2019 年 11 月 28 日
Do you have code to generate the matrices or can you upload the variables as a .mat.
Jay Vaidya
Jay Vaidya 2019 年 11 月 28 日
Yes I have a big code which generates these 2 matrices.

サインインしてコメントする。

採用された回答

Jay Vaidya
Jay Vaidya 2019 年 11 月 28 日
Thank you Stephen Cobeldick. I used ismember() function and that is excuting this in no time.
binary_probability_matrix(:,1:2) = sort(binary_probability_matrix(:,1:2),2);
A1 = matrix;
[X,Y] = ismember(matrix(:,[1,2]),binary_probability_matrix(:,[1,2]),'rows');
matrix(X,3:4) = binary_probability_matrix(Y(X),3:4 );
isequal(A1,matrix); %just to check if they are really matched.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by