array indexing with another array in multidimensional matrices

Thank you in advance for helping:
I have a multidimensional matrix (let's say 4D as example) and I want to index its values through another matrix which contains all possible combinations, as an example:
A=randi(10,5,4,3,2) %%is 4D matrix
and I want to index its "submatrices" through another vector, without performing for loops. In this specific case my indexing matrix would be:
idx=[1,1;2,1;3,1;1,2;2,2;3,2] %%is indexing matrix
through which I would like to obtain my resulting matrices:
A(:,:,1,1);
A(:,:,2,1);
A(:,:,3,1);
A(:,:,1,2);
A(:,:,2,2);
A(:,:,3,2);
by adding the last two indices with my indexing matrix:
A(:,:,B(1,:));
A(:,:,B(2,:));
A(:,:,B(3,:));
A(:,:,B(4,:));
A(:,:,B(5,:));
A(:,:,B(6,:));
but the two forms don't give the same results and I would like to code a form similar to the last one to obtain the first results, i.e. singe 2D matrices.
Thanks

 採用された回答

Cris LaPierre
Cris LaPierre 2020 年 8 月 2 日
編集済み: Cris LaPierre 2020 年 8 月 2 日

0 投票

The issue here is that, when indexing, a vector of numbers acts on a single dimension. When indexing, a comma separates dimensoins, much like in a function you use a comma to separate the different inputs. You are expecting MATLAB to 'know' you want the first column to apply to one dimiension, and the second column to another. Instead, you must explicity state the input for each dimension, separating each with a comma:
A(:,:,B(:,1),B(:,2))

8 件のコメント

gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
but that I cannot do, unfortunately in my code. Is there otherwise any possibility to reshape my multidimension matrix so that I will get a 2D matrix which would look like
vertcat(A(:,:,1,1),A(:,:,2,1),A(:,:,3,1),A(:,:,1,2),A(:,:,2,2),A(:,:,3,2));
without explicity calling every element, i.e. reshaping somehow my 5D matrix into 2D? Thank you for helping, I am having a hard time understanding this.
Cris LaPierre
Cris LaPierre 2020 年 8 月 2 日
Perhaps you have not clearly explained your problem. What do you have to work with? In you description, you mention you have A and idx (which I assumed was B).
Modifying the code you tried, you could do this.
A(:,:,B(1,1),B(1,2));
A(:,:,B(2,1),B(2,2));
A(:,:,B(3,1),B(3,2));
A(:,:,B(4,1),B(4,2));
A(:,:,B(5,1),B(5,2));
A(:,:,B(6,1),B(6,2));
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
yes, but I can have to deal with 6 or 7 or more dimensions matrices, which I eish to sort as 2D matrices, as in the EXAMPLE. Maybe I was not too clear. In any case your solution is not general, so I might wanna consider reshape with permuting, I guess. Thank you anyway
Cris LaPierre
Cris LaPierre 2020 年 8 月 2 日
編集済み: Cris LaPierre 2020 年 8 月 2 日
So you aren't needing to extract specific 2D matrices. You just want to reshape into an Nx4 matris? Yes.
C=reshape(A,[],size(A,2))
Bruno Luong
Bruno Luong 2020 年 8 月 2 日
yes, but I can have to deal with 6 or 7 or more dimensions matrices, which I eish to sort as 2D matrices
Sigh. If you think by describing incomplete question you will get generic answer, the you are very wrong and perhaps wasting time of you and others who to reply.
Cris LaPierre
Cris LaPierre 2020 年 8 月 2 日
This code will get you the same result as your vertcat statement above. It will work for any size matrix, though I have not tested it exhaustively.
sz = size(A);
B = permute(A,[1 3:length(sz) 2]);
C=reshape(B,[],size(A,2));
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
I am sorry I didn't want to get people confused in trying to address a problem I couldn't explain without an example. Your help was very appreciated, thanks.
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
I actually need to extract specific 2D matrices, but through reshaping I can manage, thanks.

サインインしてコメントする。

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2020 年 8 月 2 日
編集済み: Bruno Luong 2020 年 8 月 2 日

0 投票

A=randi(10,5,4,3,2)
idx=[1,1;2,1;3,1;1,2;2,2;3,2]
Then
sz=size(A);
Aidx = A(:,:,sub2ind(sz(3:4),idx(:,1),idx(:,2)))

6 件のコメント

gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
same problem, I have to use 2 for cycles for idx and they are growing in number as dimension grow
Bruno Luong
Bruno Luong 2020 年 8 月 2 日
編集済み: Bruno Luong 2020 年 8 月 2 日
What for-cycle? My code doesn't have any for-cycle.
If you want generic
sz = size(A);
p = size(idx,2);
c = repelem({':'},1,ndims(A)-p+1);
cidx = num2cell(idx,1);
c{end} = sub2ind(sz(end-p+1:end), cidx{:});
Aidx = A(c{:})
I hope you will know what to do with your variant multi-dimmensional array. It seems you just engage to something over the head.
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
Yes I do, thank you!
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
your last code is very nice
Bruno Luong
Bruno Luong 2020 年 8 月 2 日
I have impression you do something overly complicated.
You replace the indexing of last p-dimension by all combinations possible of the p last indexes.
This is insanely redundant, you already have your data with all the combination in the original form. Simple use permute/reshape will put them in the form you want no need those combination/indexing.
gabriele fadanelli
gabriele fadanelli 2020 年 8 月 2 日
Yes, at first I couldn't manage it, but now yes! Anyway I aso need the 2D matrices and Mr LaPierre solution perfectly fits

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by