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

2 vues (au cours des 30 derniers jours)
Sandura Shumba
Sandura Shumba le 12 Avr 2020
Commenté : Sandura Shumba le 13 Avr 2020
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

Réponse acceptée

Peng Li
Peng Li le 12 Avr 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.
  2 commentaires
Peng Li
Peng Li le 12 Avr 2020
Typing with cell phone. Sorry it Might be messy.
Sandura Shumba
Sandura Shumba le 13 Avr 2020
Thank you so much, it worked perfectly

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by