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

采纳的回答

Jan
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

更多回答(1 个)

D.Chehov
D.Chehov 2012-2-29
Dear, Jan, thank you for your kind answer,but unfortunatelly I could not use it to obtain what I need. To be more clear, I prepared this example: ----------------------------------------------------------
function example
% F is a load N1
F=[0.0000103;1523.6563491;0.0003875;-0.0000403;-0.1044340;0.0000601;-0.0000759;-0.1959430;0.0000498;-0.0000990;-0.2559150;0.0000404;-0.0001120;734.2502860;0.0003466];
Ugd=[5.1221E-5 4.6382E-5 4.1544E-5 3.6705E-5 3.1866E-5]; % displacement time history
Vgd=[0.0039514 0.0039482 0.0039449 0.0039417 0.0039385]; % velocity time history
K=[0 0 0;
341 540 0;
0 0 0];
for i=1:5
ugvg=[ Ugd(i);Vgd(i);0];
Kugvg = K*ugvg; % K is the Load2
end;
Load=F+Kugvg; %Load=Load1+Load2 disp (Load); --------------------------------------------------------------- The error is that tha matrix dimension don't agree, becouse F is[15x1], and Kugvg is 5 small vectors [3x1]. What I need is to unify it". I tried with all my knowledge but I guess that,because of the loop iteration "for-end" I could not manage.....
Thank you again for your kind help!

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by