Get multiple matrices automatically in workspace

조회 수: 3 (최근 30일)
Marwen Tarhouni
Marwen Tarhouni 2017년 1월 2일
댓글: Stephen23 2017년 1월 3일
Dear,
I run an algorithm 50 times using the for loop. I want to save the results after each iteration ie I want to have in the workspace 50 matrices but I always find the last matrix (for i = 50). I apply this loop on all the algorithm. My algorithm contains Randomized values, every execution I have a matrix. I want to have 50 matrix automatically using the For loop but I find the last one in the workspace. Do you have an idea ?
for j=1:50
X = 200 * rand(1,20) - 100;
Y = 200 * rand(1,20) - 100;
for i=1:20
dist = sqrt((X(i)-X(:)).^2+(Y(i)-Y(:)).^2);
distance=[distance dist ];
end
end
Thnaks
  댓글 수: 2
John D'Errico
John D'Errico 2017년 1월 2일
편집: John D'Errico 2017년 1월 2일
DON'T make 50 separate matrices.
You need to learn about arrays, and multi-dimensional arrays. Learn to use arrays. That is what makes MATLAB a useful tool.
Stephen23
Stephen23 2017년 1월 3일
"I want to have 50 matrix automatically using the For loop"
Don't do this. Read this to know why:

댓글을 달려면 로그인하십시오.

채택된 답변

Niels
Niels 2017년 1월 2일
편집: Niels 2017년 1월 2일
try this
for j=1:50
X = 200 * rand(1,20) - 100;
Y = 200 * rand(1,20) - 100;
for i=1:20
dist = sqrt((X(i)-X(:)).^2+(Y(i)-Y(:)).^2);
end
matrices{j}=dist;
end
with
matrices{n}
you should be able to get the matrix of the n. loop

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by