Creating a loop to search for matching values in two matrices

3 ビュー (過去 30 日間)
mcl1993
mcl1993 2016 年 8 月 10 日
コメント済み: Azzi Abdelmalek 2016 年 8 月 10 日
I have two csv files. A is 55473 x 10 and B is 484 x 3. I want to create a loop that goes through column 5 in A, and when the value in the cell equals a value in column 1 in B, i want to multiply it by column 2 and column 3 of the same row in B. I want to repeat this for all values in column 5 of A.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 10 日
You can do it without a for loop
[ii,jj]=ismember(A(:,5),B(:,1));
id=nonzeros(jj);
A(ii,5)=A(ii,5).*B(id,2).*B(id,3);

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 10 日
%-------Example-----------------
A=randi(10,55473,10);
B=randi(10,484,3);
%----------------------------------
for k=1:size(A,1)
idx= find(ismember(B(:,1),A(k,5)),1);
if ~isempty(idx);
A(k,5)=A(k,5)*B(idx,2)*B(idx,3);
end
end
  2 件のコメント
mcl1993
mcl1993 2016 年 8 月 10 日
Thanks, can you explain what the line idx = find(ismember(B(:,1),A(k,5)),1); means?
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 10 日
doc find
doc ismember

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by