How to arrange vectors 3x1 after "if" code in common vector 3*ix1?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jan
on 29 Feb 2012
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 Comments
More Answers (1)
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!