フィルターのクリア

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

1 回表示 (過去 30 日間)
okoth ochola
okoth ochola 2023 年 3 月 30 日
回答済み: Nithin Kumar 2023 年 3 月 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by