recall updated vector from a loop

I have a column vector "displacement" with 18 elements, which is updated at every iteration (Nsteps) in a loop.
I would like to obtain a matrix such that each column is the displacement vector at each iteration. How can I do this?

답변 (1개)

Joe Vinciguerra
Joe Vinciguerra 2019년 10월 23일

2 개 추천

nLoops = 5; % number of times to loop
nElem = 18; % number of elements in each vector (let's use it as long as you know what it is
combinedResults = zeros(nElem,nLoops); % it's good practice to preallocate variables generated in a loop
for i = 1:nLoops
displacement = rand(nElem,1); % I don't have your data, so I'm using rand to simulate it
combinedResults(:,i) = displacement; % Here's where it sounds like you need help. The column you save to increments with i.
end

댓글 수: 1

Guillaume
Guillaume 2019년 10월 23일
+1 for the use of good variable names, and for the preallocation.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2019년 10월 23일

댓글:

2019년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by