How to arrange vectors 3x1 after "if" code in common vector 3*ix1?
1 次查看(过去 30 天)
显示 更早的评论
Hello, I would like to aks you a question,probably a simple one, but which hinders me. I have this code: ----------------------------------------
for i=1:NUI;
if (i==1);
statement.....
elseif (i>=2 && i<=NUI-1)
ugvg=[Ugd(3,i);Vgd(3,i);0];
Kugvg=zeros(3,1);
Kugvg=K*ugvg;
elseif (i==NUI);
statement....
end;
end;
-----------------------------------------------------------
where Ugd and Vgd are global variables;
K is a matrix [3x3]; ugvg is a vector [3x1] and Kugvg is a resultant vector of the multiplication.
I'd like to create vector (array) whit dimensin 3*i x 1, which consecutively contains all small vectors Kugvg[3x1] for i=2:NUI-1.
Thanks in advance!
Regards
0 个评论
采纳的回答
Jan
2012-2-29
Kugvg = zeros(3, NUI); % Pre-allocation
iK = 0;
for i = 1:NUI
if i==1
statement.....
elseif i <= NUI-1 % i is always >= 2, if i==1 is checked before
ugvg=[Ugd(3,i);Vgd(3,i);0];
% Kugvg=zeros(3,1); Not useful here!
iK = iK + 1;
Kugvg(:, iK) = K*ugvg;
else % if i==NUI, check not required!
statement....
end
end
% Crop and reshape to [3*i x 1]:
Kugvg = Kugvg(:, 1:iK);
Kugvg = Kugvg(:);
Instead of the coarse pre-allcation with the full number of columns, you could do this:
Kugvg = zeros(3, NUI-2);
...
Kugvg(:, i-1) = K*ugvg;
...
% Then no cropping is required
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!