locate element using find function in 3D matrix
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello,
I am working in the code bellow. The main purpose of question is to determine the location of varibale B(:,:,i). respecting the 3 indeex of the matrix.
%------ the declation of matrix and choose the 9 largest value of the matrix
t = input('Enter indeex 3 of your matrix - ')
matrix2 = rand(6,6,t)
for i = 1:t
T2(:,:,i) = unique(matrix2(:,:,i)) % Determine the unique of variable matrix. tunique size 6*6*1 , 6*6*2 and 6*6*3.......6*6*t
B(:,:,i) = T2(27:36,:,i) % I choose only the last 9 nums from t unique
end
% I need to know the position of each B(:,:,i) in the main matrix2 and
% their indeex
for d = 1 : t
for j = 1:9
[a(j) b(j)] = find(matrix2(:,:,d) == B(j,:,d)); % I used the function find to locate the varivale o in the matrix
end
end
I really appreciate any help
0 commentaires
Réponse acceptée
DGM
le 27 Mar 2021
Modifié(e) : DGM
le 27 Mar 2021
I am not really sure what the end goal here is, but to just get the row/column indexes of the subset of values selected
t = input('Enter size of your matrix along dim 3 - ')
matrix2 = rand(6,6,t);
num=9; % define this as a paramter. call it whatever is appropriate
B=zeros([num t]); % preallocate B
for i = 1:t
% unique() returns a vector, not a 2D array.
% if we don't need to keep T2 for anything, don't.
% we can't guarantee the length of T2, so flip it
% that way we're not indexing from the free end
T2 = flipud(unique(matrix2(:,:,i)));
B(:,i) = T2(1:num);
end
% i'm going to throw the results in a cell array
% since there's no guarantee that the results will have a common size
locations=cell(t,num);
for d = 1:t
for j = 1:num
[row col] = find(matrix2(:,:,d) == B(j,d));
locations{d,j}=[row col];
end
end
% look at the results
celldisp(locations);
This will give the results in a cell array, with each cell containing the row and column indexes corresponding to each value in the vector B. For an array with t=1
locations{1} =
1 3
locations{2} =
2 4
locations{3} =
6 2
locations{4} =
3 1
locations{5} =
4 2
locations{6} =
2 3
locations{7} =
5 6
locations{8} =
4 5
locations{9} =
3 3
Why did I choose to use a cell array? Since you are using unique() to filter the values in matrix2, I can only assume that you expect the values in matrix2 to be potentially nonunique and that is important for your tasks. If that is the case, then the results returned by find() will be of variable length. This can be demonstrated by using randi() instead of rand():
matrix2 = randi(9,6,6,t);
In this case, we are guaranteeing nonuniqueness for the purpose of demonstration, so the output will look like this for the same size array:
locations{1} =
6 1
1 2
5 2
4 3
1 5
3 5
locations{2} =
5 4
4 6
locations{3} =
1 3
2 3
1 4
6 5
locations{4} =
4 2
6 2
3 4
4 4
6 4
2 6
5 6
locations{5} =
3 1
3 2
locations{6} =
6 3
locations{7} =
5 3
2 5
4 5
5 5
3 6
locations{8} =
1 1
2 1
4 1
5 1
3 3
locations{9} =
2 2
2 4
1 6
6 6
If on the other hand, you were only using unique() because you meant to be using sort() instead, then things could be simplified to use a numeric output array.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!