필터 지우기
필터 지우기

Creating a mtarix from results of a for loop that creates vectors

조회 수: 1 (최근 30일)
Sandura Shumba
Sandura Shumba 2020년 4월 12일
댓글: Sandura Shumba 2020년 4월 13일
Hi
I very new to Matlab and i hoped that someone could help me. I have the following for loop
for I = R
i1 = find(I, 1, 'first');
i2 = find(I, 1, 'last');
I=I(i1:i2);
xq = 1:5;
I = interp1(I,xq);
disp(I)
end
R is nxm matrix. So basicallly i perform the functions in the loop to each column in the matrix R and this results in a vector I. What i would like is to create a new matrix, each column in the matrix must be each of the verctors I created in the loop. Could you please guide me as to how to do this

채택된 답변

Peng Li
Peng Li 2020년 4월 12일
Not really understand what you intended to do. But based on your code, you could get an array by accumulating the vector you get within each loop. First, you’d better not overwrite your loop variable I. Second in your interp1 you don’t have an exact value for the original x. I guess you assume the default 1:length(I) unless this is not your intention. Let’s use newI for the interp1 output:
newI = interp1(I, xq);
D = [D newI(:)];
Before for I = R, you need also add D =[];
It’ll be better if you reallocate D first with the supposed size and assign newI to its column. E.g.
D = nan(5, size(R, 2));
for iC = 1:size(R, 2)
I = R(:, iC);
... % your interp code
D(:, iC) = newI(:);
end
You can test them. The second solution should be faster.
  댓글 수: 2
Peng Li
Peng Li 2020년 4월 12일
Typing with cell phone. Sorry it Might be messy.
Sandura Shumba
Sandura Shumba 2020년 4월 13일
Thank you so much, it worked perfectly

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by