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

1 view (last 30 days)
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

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 8 Aug 2019
lo = M_data == 1;
[ii,jj,k] = ind2sub(size(M_data),find(lo));
output = [ii,jj,k,B_data(lo)];

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!