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

2 views (last 30 days)
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

Accepted Answer

Peng Li
Peng Li on 12 Apr 2020
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by