フィルターのクリア

Creating a Matrix( 3 by N*3) from the column vector of 3 other matrices(3 by N), in vectorised operations

19 ビュー (過去 30 日間)
I start with A a N by 3 matrix generated from data, which I use to create another N by 3 matrix B , with which using a cross product I create the last N by 3 Matrix C.
Each of these matrixes rows ( or columns with permutation) are the column of my last matrix Result.
I want to take in a vectorised operation, the first row ( or column in permutated) of each of these matrixes to create my first 3 by 3 matrix, do the same for the secon, third and so on until the nth element.
which I will then transform in a 3 by 3 by N matrix with reshape.
However I'm having a lot of trouble create the Result matrix because anything that I tried ended up giving me the concatenation of all A by all B by all, and I do not know of the existence of a column operator that restarts the count if it arrives to the end( otherwise I could re- arrange the matrix using the number [A(1:3N:N)),B(N+1:3N,N)C(2N+1:3N:N)].
So in matrix representation I'm looking for this:
A=[1 ,10, 19;2, 11, 20 ;3, 12, 21]
A =
1 10 19
2 11 20
3 12 21
>> B=[4 13, 22;5,14, 23; 6, 15 ,24]
B =
4 13 22
5 14 23
6 15 24
>> C=[7 16, 25; 8 17,26; 9 18, 27]
C =
7 16 25
8 17 26
9 18 27
>> D=[1 ,4,7,10,13,16,19,22,25;2,5,8,11,14,17,20,23,26;3,6,9,12,15,18,21,24,27]
D =
1 4 7 10 13 16 19 22 25
2 5 8 11 14 17 20 23 26
3 6 9 12 15 18 21 24 27
%and then Result=Reshape(D,3,3,[])
Result=reshape(D,3,3,[])
Result(:,:,1) =
1 4 7
2 5 8
3 6 9
Result(:,:,2) =
10 13 16
11 14 17
12 15 18
Result(:,:,3) =
19 22 25
20 23 26
21 24 27
  1 件のコメント
Moatassem
Moatassem 2023 年 7 月 6 日
編集済み: Moatassem 2023 年 7 月 6 日
Question Answered by a collegue of mine, but maybe it's not optimal so I'll keep the question up!
The result is:
%Given Matrices A,B,C
A=[1 ,10, 19;2, 11, 20 ;3, 12, 21]
B=[4 13, 22;5,14, 23; 6, 15 ,24]
C=[7 16, 25; 8 17,26; 9 18, 27]
%% only if you use want to concatenate row vectors as column with no transpose (it's an expensive operation)
%A=permute(A,[2,1])
%B=permute(B,[2,1])
%C=permute(C,[2,1])
%%Actual result
D=[A;B;C];
Result=reshape(D,3,3,[])

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

回答 (2 件)

Gandham Heamanth
Gandham Heamanth 2023 年 7 月 6 日
hi Moatassem, please see my solution
% Given matrices A, B, and C
A = [1, 10, 19; 2, 11, 20; 3, 12, 21];
B = [4, 13, 22; 5, 14, 23; 6, 15, 24];
C = [7, 16, 25; 8, 17, 26; 9, 18, 27];
% Concatenate matrices A, B, and C horizontally
D = [A, B, C];
% Reshape D to obtain the desired result
Result = reshape(D.', 3, 3, [])
Result =
Result(:,:,1) = 1 4 7 10 13 16 19 22 25 Result(:,:,2) = 2 5 8 11 14 17 20 23 26 Result(:,:,3) = 3 6 9 12 15 18 21 24 27
  1 件のコメント
Moatassem
Moatassem 2023 年 7 月 6 日
編集済み: Moatassem 2023 年 7 月 6 日
Thank you for your answer!!
Maybe I was not clear, I was looking to have the numbers regrouped by elements from 1 to 9 counting by columns like D Matrix in the question.
By the way how are you able to run the code in the answer? It gives me No Code found for my comment

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


Aditya Singh
Aditya Singh 2023 年 7 月 6 日
編集済み: Aditya Singh 2023 年 7 月 6 日
Hi,
To my understanding you want to concatnate coloumn from each of the matrix.
The following code will do it,
A=[1 ,10, 19;2, 11, 20 ;3, 12, 21]
A = 3×3
1 10 19 2 11 20 3 12 21
B=[4 13, 22;5,14, 23; 6, 15 ,24]
B = 3×3
4 13 22 5 14 23 6 15 24
C=[7 16, 25; 8 17,26; 9 18, 27]
C = 3×3
7 16 25 8 17 26 9 18 27
combinedMatrix = [];
numColumns = size(A, 2);
for i = 1:numColumns
% Select the ith column from each matrix
columnA = A(:, i);
columnB = B(:, i);
columnC = C(:, i);
% Combine the columns into a single matrix
combinedMatrix = [combinedMatrix, columnA, columnB, columnC];
end
disp(combinedMatrix)
1 4 7 10 13 16 19 22 25 2 5 8 11 14 17 20 23 26 3 6 9 12 15 18 21 24 27
Result=reshape(combinedMatrix,3,3,[])
Result =
Result(:,:,1) = 1 4 7 2 5 8 3 6 9 Result(:,:,2) = 10 13 16 11 14 17 12 15 18 Result(:,:,3) = 19 22 25 20 23 26 21 24 27
  2 件のコメント
Moatassem
Moatassem 2023 年 7 月 6 日
I was looking for that result but with no loop to have vectorised operations, my collegue managed to find an answer if you are interested, maybe not optimal though, posted it as comment!
Aditya Singh
Aditya Singh 2023 年 7 月 6 日
Hi,
I changed the implementation, we create an indices vector using repmat to repeat the sequence from 1 to the number of columns, three times (to match the number of matrices A, B, and C).
Next, we use the indices vector to select the desired columns from matrices A, B, and C using matrix indexing.
A=[1 ,10, 19;2, 11, 20 ;3, 12, 21]
A = 3×3
1 10 19 2 11 20 3 12 21
B=[4 13, 22;5,14, 23; 6, 15 ,24]
B = 3×3
4 13 22 5 14 23 6 15 24
C=[7 16, 25; 8 17,26; 9 18, 27]
C = 3×3
7 16 25 8 17 26 9 18 27
% Get the number of columns in matrix A
numColumns = size(A, 2);
% Create indices for selecting columns from A, B, and C
indices = repmat(1:numColumns, 1, 1);
% Select the desired columns from A, B, and C using indices
selectedColumns = [A(:, indices); B(:, indices); C(:, indices)];
% Reshape the selected columns into a single matrix
Result=reshape(selectedColumns,3,3,[])
Result =
Result(:,:,1) = 1 4 7 2 5 8 3 6 9 Result(:,:,2) = 10 13 16 11 14 17 12 15 18 Result(:,:,3) = 19 22 25 20 23 26 21 24 27
Hope this helps!

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

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by