how to store vectors into matrix

93 ビュー (過去 30 日間)
amina amzgh
amina amzgh 2020 年 8 月 19 日
回答済み: Image Analyst 2020 年 8 月 20 日
I have a vector which takes different values ​​at each iteration I would like to store the result of the vectors in a matrix example iteration 1 A = [1 2 3 4] iteration 2 A = [5 6 7 8] so I will want a result matrix: result = [1 2 3 4; 5 6 7 8]

回答 (5 件)

KSSV
KSSV 2020 年 8 月 19 日
m = 10 ; n = 4 ;
A = zeros(m,n) ;
for i = 1:m
A(i,:) = rand(1,n) ;
end

David Hill
David Hill 2020 年 8 月 19 日
for k=1:100
A(k,:)=randi(100,1,4);%your vectors
end

Image Analyst
Image Analyst 2020 年 8 月 19 日
Try this:
result = zeros(numIterations, 4);
for k = 1 : numIterations
% Get A somehow, then
result(k, :) = A;
end

Ruger28
Ruger28 2020 年 8 月 19 日
編集済み: Ruger28 2020 年 8 月 19 日
A = [1 2 3 4];
B = [5 6 7 8];
C = [A;B]
In a loop:
Sum_Matrix = []; % pre-allocate if you know how many total elements in array you'll have
A = [1 2 3 4];
for ii = 1:100 % number of iterations
% whatever math to get you your vector
B = A * ii; % example
Sum_Matrix = [Sum_Matrix;B];
end
  2 件のコメント
amina amzgh
amina amzgh 2020 年 8 月 19 日
it works perfectly , i just donc need to multiply ii by A ....
i really thank u a lot...........
Ruger28
Ruger28 2020 年 8 月 19 日
You're welcome.

サインインしてコメントする。


Image Analyst
Image Analyst 2020 年 8 月 20 日
If the "A" is really as predictable as you implied in your comment to Ruger28 (and not general as I assumed in my Answer), and you want a vectorized way to do it, you can use reshape:
numIterations = 5; % Whatever you want/need.
result = reshape([1 : numIterations*4]', 4, [])'
You get:
result =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Of course, this vectorized version is the more MATLAB-y way to do it. No for loops are needed at all. (This way also uses your preferred and better variable name of "result".)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by