How to make a column matrix from multi-dimensional matrix with several rows

1 view (last 30 days)
Hello, allow me inquire something little. Suppose i have matrix A which is100 by 30 matrix. I want to make a n by 1 matrix by transposig each row from A then concatenating to form B. Example;
A=[1 2 3 4 5;60 5 7 89 9;4 5 7 8 9;6 80 32 12 11];
B=[A(1,:);A(2,:);A(3,:);A(4,:)];
Now the sample program above was simple becaue it involved only a few row which can easily be computed, supposei have larger mtrix as the one given in the question in paragraph 1, how can I go about it? Thank you sir/ma'am.

Accepted Answer

Nithin Kumar
Nithin Kumar on 30 Mar 2023
Hi Okoth,
I understand that you are trying to convert a multi-dimensional matrix into a column matrix by transposing each row of the multi-dimensional matrix.
You can convert a multi-dimensional matrix into a matrix of required dimensions using "reshape". Kindly refer the following link to know more about the "reshape" function.
The following code helps you to generate the required column matrix :
A = randi([1,20],3,3) % generating a 3x3 matrix within the range "1 to 20"
A = 3×3
1 15 20 20 14 8 1 18 18
B = reshape(A.',[],1); % Transposing the matrix "A" row-wise into a Column Matrix
disp(B); % displaying the resultant matrix "B"
1 15 20 20 14 8 1 18 18
I hope this answer helps you.

More Answers (1)

Adithya
Adithya on 30 Mar 2023
Edited: Adithya on 30 Mar 2023
Suppose if matrix A = [1 2 3;4 5 6] , u want a matrix B to be equal to [1 4;2 5;3 6] ie transpose each row to obatain n*1 matrix and then append to B.
U can do this by making use of simple for loop , below is the implementation:
A=[1 2 3; 4 5 6];
n = size(A,1);
B=[];
for i=1:n
C = A(i,:);
B = horzcat(B,C');
end
disp(B);

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!