for構文
    5 views (last 30 days)
  
       Show older comments
    
CPが477000行364列,Coが364行2列のもので,以下の処理したときに。aaの刻み分の列のデータが出てくるのかと思いきや,477000行1列のファイルしか吐き出されませんでした。間違いを指摘していただければ幸いです。
for aa = 0:28:336
    g1 = (CP(:,aa+28).*Co(aa+28,2)'.*cos(pi/4)+CP(:,aa+1).*Co(aa+1,2)'+CP(:,aa+2).*Co(aa+2,2)'+CP(:,aa+3).*Co(aa+3,2)'+CP(:,aa+4).*Co(aa+4,2)'+CP(:,aa+5).*Co(aa+5,2)'+CP(:,aa+6).*Co(aa+6,2)'+CP(:,aa+7).*Co(aa+7,2)'.*cos(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    g2 = (CP(:,aa+7).*Co(aa+7,2)'.*sin(pi/4)+CP(:,aa+8).*Co(aa+8,2)'+CP(:,aa+9).*Co(aa+9,2)'+CP(:,aa+10).*Co(aa+10,2)'+CP(:,aa+11).*Co(aa+11,2)'+CP(:,aa+12).*Co(aa+12,2)'+CP(:,aa+13).*Co(aa+13,2)'+CP(:,aa+14).*Co(aa+14,2)'.*sin(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    g3 = (CP(:,aa+14).*Co(aa+14,2)'.*cos(pi/4)+CP(:,aa+15).*Co(aa+15,2)'+CP(:,aa+16).*Co(aa+16,2)'+CP(:,aa+17).*Co(aa+17,2)'+CP(:,aa+18).*Co(aa+18,2)'+CP(:,aa+19).*Co(aa+19,2)'+CP(:,aa+20).*Co(aa+20,2)'+CP(:,aa+21).*Co(aa+21,2)'.*cos(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    g4 = (CP(:,aa+21).*Co(aa+21,2)'.*sin(pi/4)+CP(:,aa+22).*Co(aa+22,2)'+CP(:,aa+23).*Co(aa+23,2)'+CP(:,aa+24).*Co(aa+24,2)'+CP(:,aa+25).*Co(aa+25,2)'+CP(:,aa+26).*Co(aa+26,2)'+CP(:,aa+27).*Co(aa+27,2)'+CP(:,aa+28).*Co(aa+28,2)'.*sin(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    fullFx = g1 - g3;
    fullFy = g2 - g4;
    writematrix(fullFx,'dx_4layers.dat');
    writematrix(fullFy,'dx_4layers.dat');
end
0 Comments
Accepted Answer
  Dyuman Joshi
      
      
 on 11 Apr 2024
        That is because you are overwriting the files in each iteration of the for loop.
Pre-allocate the output to assign data to it and save the data after the for loop - 
vec = 0:28:336;
n = numel(vec);
fullFx = zeros(size(CP,1), n);
fullFy = zeros(size(CP,1), n);
for aa = vec
    g1 = (CP(:,aa+28).*Co(aa+28,2)'.*cos(pi/4)+CP(:,aa+1).*Co(aa+1,2)'+CP(:,aa+2).*Co(aa+2,2)'+CP(:,aa+3).*Co(aa+3,2)'+CP(:,aa+4).*Co(aa+4,2)'+CP(:,aa+5).*Co(aa+5,2)'+CP(:,aa+6).*Co(aa+6,2)'+CP(:,aa+7).*Co(aa+7,2)'.*cos(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    g2 = (CP(:,aa+7).*Co(aa+7,2)'.*sin(pi/4)+CP(:,aa+8).*Co(aa+8,2)'+CP(:,aa+9).*Co(aa+9,2)'+CP(:,aa+10).*Co(aa+10,2)'+CP(:,aa+11).*Co(aa+11,2)'+CP(:,aa+12).*Co(aa+12,2)'+CP(:,aa+13).*Co(aa+13,2)'+CP(:,aa+14).*Co(aa+14,2)'.*sin(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    g3 = (CP(:,aa+14).*Co(aa+14,2)'.*cos(pi/4)+CP(:,aa+15).*Co(aa+15,2)'+CP(:,aa+16).*Co(aa+16,2)'+CP(:,aa+17).*Co(aa+17,2)'+CP(:,aa+18).*Co(aa+18,2)'+CP(:,aa+19).*Co(aa+19,2)'+CP(:,aa+20).*Co(aa+20,2)'+CP(:,aa+21).*Co(aa+21,2)'.*cos(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    g4 = (CP(:,aa+21).*Co(aa+21,2)'.*sin(pi/4)+CP(:,aa+22).*Co(aa+22,2)'+CP(:,aa+23).*Co(aa+23,2)'+CP(:,aa+24).*Co(aa+24,2)'+CP(:,aa+25).*Co(aa+25,2)'+CP(:,aa+26).*Co(aa+26,2)'+CP(:,aa+27).*Co(aa+27,2)'+CP(:,aa+28).*Co(aa+28,2)'.*sin(pi/4))./ (1000.^2).* qba .* ((Bfull/Bmodel).^2) ;
    %Add data to columns of the preallocated variables
    fullFx(:, (aa/28)+1) = g1 - g3;
    fullFy(:, (aa/28)+1) = g2 - g4;
end
writematrix(fullFx,'dx_4layers.dat');
writematrix(fullFy,'dx_4layers.dat');
3 Comments
More Answers (0)
See Also
Categories
				Find more on ワークスペース変数と MAT ファイル 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!
