Coordinates of a 3 by 3 by 3 array

12 vues (au cours des 30 derniers jours)
Phillip Smith
Phillip Smith le 5 Mar 2020
Modifié(e) : Stephen23 le 5 Mar 2020
Hey,
So I've made a 3 dimentional array
yy=zeros(3,3,3);
and I'm trying to find the position of the random 1that I have slapped in there. In this instance, the 1 is here, on the 3rd 'page':
yy(:,:,3) =
0 1 0
0 0 0
0 0 0
I now want to find the coordinates of the 1, to do this I use:
[x,y,z] = find(yy(1:3,1:3,1:3));
C = [x,y,z];
And now this will return:
C =
1 8 1
It doesn't make sense to me how the position is numbered, surely the position should be (2,3,3)?
If anyone can shead some light on how this coordinated numbering system works in 3D arrays I'll be forever greatful!
Many Thanks,
Phill
  1 commentaire
Stephen23
Stephen23 le 5 Mar 2020
Modifié(e) : Stephen23 le 5 Mar 2020
"It doesn't make sense to me how the position is numbered, surely the position should be (2,3,3)?"
It is not clear why you expect the output to be (2,3,3): you placed that solitary 1 in the 1st row of the 2nd column of the 3rd page, so (1,2,3) would actually match the example that you have shown.

Connectez-vous pour commenter.

Réponses (2)

Stephen23
Stephen23 le 5 Mar 2020
Modifié(e) : Stephen23 le 5 Mar 2020
Solution
For >2 dimensions you need to use ind2sub (as the find documentation also mentions):
>> yy = zeros(3,3,3);
>> yy(1,2,3) = 1;
>> [row,col,page] = ind2sub(size(yy),find(yy))
row = 1
col = 2
page = 3
Explanation
Read the find documentation and you would learn that it does NOT return the indices of an arbitrary number of dimensions like you tried to do:
[dim1,dim2,dim3,dim4,...] = find() % NOT CORRECT SYNTAX!
In fact it is limited to two output indices at most, where the second output index encodes all trailing dimensions (in a way that is rarely of use to anyone...), and the third output is not an index at all. For three outputs the documentation gives this explanation: "[row,col,v] = find(___) also returns vector v, which contains the nonzero elements of X", and further down it also explains that "If X is a multidimensional array with N > 2, then col is a linear index over the N-1 trailing dimensions of X".
Using more appropriate variable names makes this clearer:
>> [row,idx,val] = find(yy) % without that pointless indexing into yy.
row = 1
idx = 8
val = 1
We can check if find behaved according to the documentation:
  • row is clearly the row of the 1 in your array.
  • idx is a linear index encoding all trailing dimensions, which is easy to determine yourself: start counting the array elements horizontally (because the rows are excluded from this index): three 0's on page 1 + three 0's on page 2 + one 0 on page 3 + finally your 1, which gives 8 elements.
  • val is the vector of non-zero elements, which in your example will be that solitary 1.

BobH
BobH le 5 Mar 2020
Modifié(e) : BobH le 5 Mar 2020
yy=zeros(3,3,3);
yy(1,2,3) = 1
yy(:,:,1) =
0 0 0
0 0 0
0 0 0
yy(:,:,2) =
0 0 0
0 0 0
0 0 0
yy(:,:,3) =
0 1 0
0 0 0
0 0 0
[x y z]=find(yy);
Think of the three pages of 3x3 as being side-by-side. Your 1 is in the first row (x) eighth column (y). The thrid output (z) returned by find doesn't play a part in the indexing, it's a logical showing there was a non-zero at that location.
yy(1,8)
ans =
1

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by