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.
0 个评论
采纳的回答
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"
B = reshape(A.',[],1); % Transposing the matrix "A" row-wise into a Column Matrix
disp(B); % displaying the resultant matrix "B"
I hope this answer helps you.
0 个评论
更多回答(1 个)
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!