Creating a mtarix from results of a for loop that creates vectors
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!