Matlab 将数据存储在for和if循环中的矩阵中
我在for和if循环中存储矩阵中的数据时遇到问题 结果只给出了最后一次迭代的最后一个值。我要所有的钱 将所有迭代的结果存储在矩阵序列中 以下是我的代码示例:Matlab 将数据存储在for和if循环中的矩阵中,matlab,if-statement,for-loop,storing-data,Matlab,If Statement,For Loop,Storing Data,我在for和if循环中存储矩阵中的数据时遇到问题 结果只给出了最后一次迭代的最后一个值。我要所有的钱 将所有迭代的结果存储在矩阵序列中 以下是我的代码示例: clear all clc %%%%%%%%%%%%%% for M=1:3; for D=1:5; %%%%%%%%%%%%%% if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D && D <
clear all
clc
%%%%%%%%%%%%%%
for M=1:3;
for D=1:5;
%%%%%%%%%%%%%%
if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D && D <= 5))
U1=[5 6];
else
U1=[0 0];
end
% desired output:
% U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]
%%%%%%%%%%%%%%
if (M == 1) && (D==4) || ((M == 3) && (D == 1))
U2=[8 9];
else
U2=[0 0];
end
% desired output:
% U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]
%%%%%%%%%%%%%%
if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D && D <= 5))
U3=[2 6];
else
U3=[0 0];
end
% desired output:
% U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]
%%%%%%%%%%%%%%
end
end
全部清除
clc
%%%%%%%%%%%%%%
对于M=1:3;
D=1:5时;
%%%%%%%%%%%%%%
如果((M==1)&(D您每次编写UX=[xy];
时都在覆盖您的矩阵
如果要追加数据,请预先分配矩阵并在每次分配新值时指定矩阵索引,或写入UX=[UX X Y];
以直接在矩阵末尾追加数据
clear all
clc
U1=[];
U2=[];
U3=[];
for M=1:3
for D=1:5
if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D && D <= 5))
U1=[U1 5 6];
else
U1=[U1 0 0];
end
% desired output:
% U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]
if (M == 1) && (D==4) || ((M == 3) && (D == 1))
U2=[U2 8 9];
else
U2=[U2 0 0];
end
% desired output:
% U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]
if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D && D <= 5))
U3=[U3 2 6];
else
U3=[U3 0 0];
end
% desired output:
% U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]
end
end
全部清除
clc
U1=[];
U2=[];
U3=[];
对于M=1:3
对于D=1:5
如果((M==1)&(D您每次编写UX=[xy];
时都在覆盖您的矩阵
如果要追加数据,请预先分配矩阵并在每次分配新值时指定矩阵索引,或写入UX=[UX X Y];
以直接在矩阵末尾追加数据
clear all
clc
U1=[];
U2=[];
U3=[];
for M=1:3
for D=1:5
if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D && D <= 5))
U1=[U1 5 6];
else
U1=[U1 0 0];
end
% desired output:
% U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]
if (M == 1) && (D==4) || ((M == 3) && (D == 1))
U2=[U2 8 9];
else
U2=[U2 0 0];
end
% desired output:
% U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]
if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D && D <= 5))
U3=[U3 2 6];
else
U3=[U3 0 0];
end
% desired output:
% U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]
end
end
全部清除
clc
U1=[];
U2=[];
U3=[];
对于M=1:3
对于D=1:5
如果((M==1)&(D您可以完全避免循环:
[M,D] = meshgrid(1:3,1:5);
M = M(:)'; D = D(:)';
idx1 = ( M==1 & D<=3 ) | ( M== 3 & 2<=D & D<=5 );
idx2 = ( M==1 & D==4) | ( M==3 & D==1 );
idx3 = ( M==1 & D==5 ) | ( M==2 & 1<=D & D<=5 );
U1 = bsxfun(@times, idx1, [5;6]); U1 = U1(:)';
U2 = bsxfun(@times, idx2, [8;9]); U2 = U2(:)';
U3 = bsxfun(@times, idx3, [2;6]); U3 = U3(:)';
[M,D]=meshgrid(1:3,1:5);
M=M(:)';D=D(:)';
idx1=(M==1&D您可以完全避免循环:
[M,D] = meshgrid(1:3,1:5);
M = M(:)'; D = D(:)';
idx1 = ( M==1 & D<=3 ) | ( M== 3 & 2<=D & D<=5 );
idx2 = ( M==1 & D==4) | ( M==3 & D==1 );
idx3 = ( M==1 & D==5 ) | ( M==2 & 1<=D & D<=5 );
U1 = bsxfun(@times, idx1, [5;6]); U1 = U1(:)';
U2 = bsxfun(@times, idx2, [8;9]); U2 = U2(:)';
U3 = bsxfun(@times, idx3, [2;6]); U3 = U3(:)';
[M,D]=meshgrid(1:3,1:5);
M=M(:)';D=D(:)';
idx1=(M==1&D你能用简单的例子告诉我怎么做吗?regardsthanks Aabaz,但U1、U2和U3没有定义,它给出了错误:??没有定义的函数或变量“U1”。好的,太好了,谢谢Aabaz。regardsthanks请用简单的例子告诉我怎么做。regardsthanks Aabaz,但U1、U2和U3没有定义,它给出了错误r:???未定义的函数或变量“U1”。好的,很好,谢谢Aabaz。regardsthanks Amro,很好的代码。我的最终目标是找到U=U1+U2+U3,我们能直接找到这个U而不找到U1、U2和U3吗?Hanks Amro,很好的代码。我的最终目标是找到U=U1+U2+U3,我们能直接找到这个U而不找到U1、U2和U3吗