Coordinates of a 3 by 3 by 3 array
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2020-3-5
编辑:Stephen23
2020-3-5
"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.
回答(2 个)
Stephen23
2020-3-5
编辑:Stephen23
2020-3-5
Solution
>> 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.
0 个评论
BobH
2020-3-5
编辑:BobH
2020-3-5
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!