Problem with matrix indexing

3 ビュー (過去 30 日間)
AlexRD
AlexRD 2021 年 4 月 14 日
コメント済み: AlexRD 2021 年 4 月 24 日
I'm having problems getting a linear index to return the results in the form that i want.
Suppose we have three matrices:
Matrix A: 100x81
Matrix B: 9x20
Index: 49x9
I want to multiply A with B, but only the parts that are referred by the index. If we have Matrix A indexed like so:
A(:, Index), we get a 100x441 result, which is partially what i want. The 441 result is basically all of the results from A in linear index sequence (9*49). What i want is for the 49 dimension to get concatenated vertically (4900x9) instead of horizontally (100x441), like so:
What it returns (100x441)
1 2 3 ... 441
... (100)
What i want it to return (4900x9)
1 2 3 ... 9
... (4900)
My solution so far was to use reshape to get the matrix in the form that i wish, which is concatenated across the columns instead of rows, like so:
X = reshape(A(:, Index), 100*49, 9) * B;
I then reshape X again to return it to the form i want:
X = reshape(X, 100, 49, 20);
The problem with this solution is that reshape takes some processing power, and i think that it's possible to manipulate the index in a way that returns the 4900x9 row directly, and that would be way more efficient. Is this possible?
In summary:
I want to multiply indexed A with B, and have it return a 100x49x20 result in the fastest way possible.

採用された回答

Jan
Jan 2021 年 4 月 22 日
The CPU time of the reshape command is very tiny, because it only checks if the number of elements is not changed and then it replaces the size vector. reshape does not modify or move the contents of the the array in the RAM, because it does not change the order of element.
If your code does, what you want, it is a very efficient solution:
X = reshape(A(:, Index), 100*49, 9) * B;
X = reshape(X, 100, 49, 20);
  1 件のコメント
AlexRD
AlexRD 2021 年 4 月 24 日
Hey Jan, thanks for the reply.
I've created another question that better explains this issue, if you want to have a look: https://www.mathworks.com/matlabcentral/answers/812425-how-does-matlab-implement-the-operator

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

その他の回答 (1 件)

Hrishikesh Borate
Hrishikesh Borate 2021 年 4 月 19 日
Hi,
It’s my understanding that you are trying to construct a matrix of size 4900x9 by extracting the values from matrix A using indices defined in Index matrix.
Following is the code for the same :-
A = rand(100,81);
Index = randi(size(A,2),49,9);
output = zeros(size(A,1)*size(Index,1),size(Index,2));
for i=1:size(Index,1)
output((i-1)*size(A,1)+1:i*size(A,1),:) = A(:,Index(i,:));
end
For more information, refer to array indexing .
  1 件のコメント
AlexRD
AlexRD 2021 年 4 月 22 日
Hello Hrishikesh, thanks for the answer. I forgot to mention, i avoided loops because direct matrix manipulation is much quicker. My method, for example, handles all of that without loops, but the one problem with it is the reshape at the end.
I think there's a way of doing this without using loops and with only one reshape, but i'm unsure how.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by