Finding indices of array elements in 3d matrix

4 views (last 30 days)
Suppose, there is a 3d matrix M.
M(:,:,1) =
Columns 1 through 8
0.4824 0.4824 0.4824 0.4824 0.4863 0.4902 0.4941 0.4941
0.4824 0.4863 0.4863 0.4902 0.4941 0.4980 0.4980 0.5020
0.4824 0.4863 0.4863 0.4902 0.4941 0.4980 0.5020 0.5059
M(:,:,2) =
Columns 1 through 8
0.5412 0.5412 0.5412 0.5412 0.5451 0.5490 0.5529 0.5529
0.5412 0.5451 0.5451 0.5490 0.5529 0.5569 0.5569 0.5608
0.5412 0.5451 0.5451 0.5490 0.5529 0.5569 0.5608 0.5647
M(:,:,3) =
Columns 1 through 8
0.3725 0.3725 0.3725 0.3725 0.3765 0.3804 0.3843 0.3843
0.3725 0.3765 0.3765 0.3804 0.3843 0.3882 0.3882 0.3922
0.3725 0.3765 0.3765 0.3804 0.3843 0.3882 0.3922 0.3961
there is another 2D matrix 'V' having 3 columns of values.
V= 0.482352941176471 0.541176470588235 0.372549019607843
0.490196078431373 0.549019607843137 0.380392156862745
0.478431372549020 0.537254901960784 0.376470588235294
Now, we need to find the indices of all the column values of V in M row wise that appear all together. I have implemented as below.
for pt= 1:length(V)
[x0,y0,z0]=
find(M(:,:,1)==V(pt,1,:)& M(:,:,2)==V(pt,2,:)& M(:,:,3)==V(pt,3,:));
It does not show all the indices in array. But, separately finding x0, y0,z0 works. V need not to be of same dimension. Whoever can guide, please, solve it as soon as possible.

Accepted Answer

Stephen23
Stephen23 on 13 May 2017
Edited: Stephen23 on 13 May 2017
A stab in the dark, but perhaps something like this?:
>> W = permute(V,[4,3,2,1]);
>> tol = 0.0001;
>> idx = any(all(abs(bsxfun(@minus,M,W))<tol,3),4)
idx =
1 1 1 1 0 1 0 0
1 0 0 1 0 0 0 0
1 0 0 1 0 0 0 0
Where idx shows the where all planes of M match any row of V.
  6 Comments
Stephen23
Stephen23 on 5 Jul 2017
@Anupam Saikia: I have no idea what you mean by "spmd" in relation to MATLAB. If you want to run that code on multiple images then put it a function and call it in a loop, making sure that you preallocate the output array. This would be about the most efficient solution using pure MATLAB code.
Anupam  Saikia
Anupam Saikia on 5 Jul 2017
Edited: Stephen23 on 5 Jul 2017
What should be done here (Starting matlabpool using the 'local' configuration ... connected to 4 labs.)
>> spmd
srcFile=dir('C:\Users\psychea\Documents\MATLAB\Dataset\*.jpg');
filename=[];
for jj=(1:length(srcFile))
filename=strcat('C:\Users\psychea\Documents\MATLAB\Dataset\',srcFile(jj).name);
X1=im2double(imread(filename));
idx1=all(abs(bsxfun(@minus,X1,PM))<tol,3);
[Rpa,Cpa,~,Zpa]=ind2sub(size(idx1),find(idx1));
AllCat_par=[Rpa,Cpa,Zpa];
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!