Finding indices of array elements in 3d matrix

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

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

It's ok from one angle, but what serves my purpose is that all the indices of three column elements in Matrix M should be shown corresponding to 3 column elements of matrix V. It means indices will be returned only if all the three elements resemble plane wise. First column element of V resembles 1st plane element of M and such others up to 3rd column. I have no problem if it requires cell array Thank you
"It means indices will be returned only if all the three elements resemble plane wise"
My code does exactly that: if you look at idx, it has value 1 where all planes of M match one row of V.
If you want to know which rows of V have been matched to planes of M, then you can do this:
>> W = permute(V,[4,3,2,1]);
>> tol = 0.0001;
>> A = all(abs(bsxfun(@minus,M,W))<tol,3);
>> [R,C,~,Z] = ind2sub(size(A),find(A));
>> [R,C,Z]
ans =
1 1 1
2 1 1
3 1 1
1 2 1
1 3 1
1 4 1
2 4 2
3 4 2
1 6 2
The first two columns are the row and column indices of M (and these are the locations of ones in idx in my answer). The last column tells the row of V, so we can see the only rows one and two were matched to the planes of M. A quick visual inspection confirms that this is correct.
Thank you so much Mr. Cobeldick. It has become helpful for me. Thanks once again.
Can I use this function in spmd having multiple images in place of M
@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.
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)

Categories

Asked:

on 13 May 2017

Edited:

on 5 Jul 2017

Community Treasure Hunt

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

Start Hunting!