How to save a vector in a for loop

10 ビュー (過去 30 日間)
John Miller
John Miller 2020 年 8 月 20 日
回答済み: KSSV 2020 年 8 月 20 日
I have a for loop which creates and plots a vector y. I now want to save each generated vector so I can compare them later. Does anyone have an idea? It kind of looks like this (very simplified)
for j=1:5
y = rand(5,3)
subplot(j)
plot(y)
hold on
end
now I want to compare the i-th row of y with the i-th row of all the other y's
Does anyone have an idea? All help appreciated!

回答 (1 件)

KSSV
KSSV 2020 年 8 月 20 日
% To save a scalar in a loop to vector
n = 10 ;
A = zeros(n,1) ;
for i = 1:n
A(i) = rand ;
end
% To save a vector in loop to a 2D matrix
n = 10 ; m = 5 ;
A = zeros(m,n) ;
for i = 1:m
A(i,:) = rand(1,n)
end
% To save a matrix in a loop to a 3D matrix
m = 10 ; n = 10 ; p = 5 ;
A = zeros(m,n,p) ;
for i = 1:p
A(:,:,i) = rand(m,n) ;
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by