Creating a Matrix( 3 by N*3) from the column vector of 3 other matrices(3 by N), in vectorised operations
Show older comments
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 Comment
Answers (2)
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, [])
1 Comment
Aditya Singh
on 6 Jul 2023
Edited: Aditya Singh
on 6 Jul 2023
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]
B=[4 13, 22;5,14, 23; 6, 15 ,24]
C=[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)
Result=reshape(combinedMatrix,3,3,[])
2 Comments
Moatassem
on 6 Jul 2023
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]
B=[4 13, 22;5,14, 23; 6, 15 ,24]
C=[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,[])
Hope this helps!
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!