Find values in a 3D array based on values in a seperate 3D array

1 回表示 (過去 30 日間)
KS
KS 2019 年 8 月 8 日
コメント済み: KS 2019 年 8 月 8 日
I have two seperate 3D arrays that have equivalent sizes. One array (M_data) is binary. If the value of an element =1, I'd like to save the value of the corresponding position from the second array (B_data) in a matrix. So far, it looks like my code is giving me the i,j,k positions of M_data that equal 1, but it isn't referencing the B_data and it's not saving any of the data to a matrix. It's been a while since I've worked in Matlab and frankly I was never much of an expert, so any help would be much appreciated.
for i=1:size(M_data,1)
for j=1:size(M_data,2)
for k=1:size(M_data,3)
if M_data(i,j,k)==1
output= i,j,k, B_data(i,j,k);
end
end
end
end

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 8 日
lo = M_data == 1;
[ii,jj,k] = ind2sub(size(M_data),find(lo));
output = [ii,jj,k,B_data(lo)];
  1 件のコメント
KS
KS 2019 年 8 月 8 日
Thank you Andrei!! That worked perfectly.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by