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

조회 수: 1 (최근 30일)
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.

채택된 답변

Nithin Kumar
Nithin Kumar 2023년 3월 30일
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.

추가 답변 (1개)

Adithya
Adithya 2023년 3월 30일
편집: Adithya 2023년 3월 30일
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);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by