How to Obtain a Portion of an n-dimensioned Matrix

I have a 6 dimensional matrix with dimensions 16x20x22x6x3x13. I identified specific elements of that matrix with the following:
y = find(spec_neg(:,:,:,:,:,1) ~= 0);
Since y is now the indices of the spec_neg and I would like these in matrix subscript form, I determined the subscripts by doing the following:
[i,j,k,l,m,n] = ind2sub([16,20,22,6,3,13],y);
and then concatenated these subscripts into matrix form:
y_ind = cat(2,i,j);
y_ind = cat(2,y_ind,k);
y_ind = cat(2,y_ind,l);
y_ind = cat(2,y_ind,m);
y_ind = cat(2,y_ind,n);
I want to obtain the values of spec_neg that correspond to the subscript values of y_ind but when I do the following:
spec_neg(y_ind)
I get all zeros so that is obviously not the values in y.
What am I doing wrong?

1 Comment

Since y is now the indices of the spec_neg and I would like these in matrix subscript form
Be careful. The vector y does not contain indices of spec_neg. It contains indices of the 5-dimensional array spec_neg(:,:,:,:,:,1). The two would not coincide if you were indexing a different section like spec_neg(:,:,:,:,:,10).
So, which is it that you want?

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 20 Nov 2014
Edited: Matt J on 20 Nov 2014
I would extract the values prior to extracting the indices, as below. Notice also that you don't have to do all those painful multiple calls to cat(2,...):
s=spec_neg(:,:,:,:,:,1);
idx=logical(s);
values=s(idx);
[c{1:5}]=ind2sub(size(s), find(idx));
y_ind=cat(2,c{:});
Notice also that, while spec_neg is 6-dimensional, spec_neg(:,:,:,:,:,1) is only 5-dimensional, so I think you really want 5 columns in y_ind.

5 Comments

I had to take a little time to think on this. I see now that spec_neg(:,:,:,:,:,1) is only 5 dimensional. I didn't think about it when I did this originally.
I'm having a hard time understanding how parts of the last two lines work in your solution. I understand the ind2sub and cat functions but I don't understand how the bracket and brace operators work. Can you help me understand them?
[c{1:5}]=ind2sub(size(s), find(idx));
y_ind=cat(2,c{:});
Brackets and braces allow for "comma-separated list expansion" both on inputs and outputs:
There's a learning curve but very powerful and arguable necessary for working with n-dimensional arrays.
In the above example, I want to obtain the 13 values in the 6th dimension of spec_neg that correspond to the indices in y_ind. Why does the following not work?
spec_neg(y_ind)
I would also like those values concatenated with y_ind to yield a 2 dimensional matrix.
Why does the following not work?
Because y_ind is a concatenation of subscripts, which is not a legal kind of indexing in MATLAB. Only when y_ind is a linear or logical index array can it be used to pull out data from an array with the single argument syntax,
spec_neg(y_ind)
Subscripts must be used in a comma-separated syntax
spec_neg(I,J,K,...)
Furthermore, only rectangular sub-matrices and sub-arrays can be extracted with subscripts. To extract an arbitrary list of values, you must again use logical or linear indexing.

Sign in to comment.

More Answers (0)

Categories

Asked:

Jim
on 20 Nov 2014

Edited:

on 16 Dec 2014

Community Treasure Hunt

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

Start Hunting!