Find values in a 3D array based on values in a seperate 3D array
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 8 Aug. 2019
lo = M_data == 1;
[ii,jj,k] = ind2sub(size(M_data),find(lo));
output = [ii,jj,k,B_data(lo)];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!