Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将指定的索引设置为零_Matlab - Fatal编程技术网

Matlab 将指定的索引设置为零

Matlab 将指定的索引设置为零,matlab,Matlab,我有两个大小相同的矩阵(x1和x2)。我想使用x1中等于零的元素将x2中的sames元素设置为零 我现在得到的非工作解决方案如下: [i j] = find(x1 == 0); x2(i,j) = 0; 我还有一个可行的解决方案: [i j] = find(x1 == 0); for n=1:length(i) x2(i(n),j(n)) = 0; end 谢谢 试试x2(x1==0)=0。例如: >> x1 = rand(5, 5) x1 = 0.4229 0.6

我有两个大小相同的矩阵(
x1
x2
)。我想使用
x1
中等于零的元素将
x2
中的sames元素设置为零

我现在得到的非工作解决方案如下:

[i j] = find(x1 == 0);
x2(i,j) = 0;
我还有一个可行的解决方案:

[i j] = find(x1 == 0);
for n=1:length(i)
    x2(i(n),j(n)) = 0;
end
谢谢

试试
x2(x1==0)=0
。例如:

>> x1 = rand(5, 5) x1 = 0.4229 0.6999 0.5309 0.9686 0.7788 0.0942 0.6385 0.6544 0.5313 0.4235 0.5985 0.0336 0.4076 0.3251 0.0908 0.4709 0.0688 0.8200 0.1056 0.2665 0.6959 0.3196 0.7184 0.6110 0.1537 >> x2 = randi(2, 5, 5) - 1 x2 = 0 1 1 0 1 0 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 >> x1(x2 == 0) = 0 x1 = 0 0.6999 0.5309 0 0.7788 0 0.6385 0 0 0.4235 0.5985 0.0336 0.4076 0.3251 0 0 0.0688 0.8200 0.1056 0.2665 0.6959 0 0 0 0 >>x1=兰特(5,5) x1= 0.4229 0.6999 0.5309 0.9686 0.7788 0.0942 0.6385 0.6544 0.5313 0.4235 0.5985 0.0336 0.4076 0.3251 0.0908 0.4709 0.0688 0.8200 0.1056 0.2665 0.6959 0.3196 0.7184 0.6110 0.1537 >>x2=randi(2,5,5)-1 x2= 0 1 1 0 1 0 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 >>x1(x2==0)=0 x1= 0 0.6999 0.5309 0 0.7788 0 0.6385 0 0 0.4235 0.5985 0.0336 0.4076 0.3251 0 0 0.0688 0.8200 0.1056 0.2665 零点六九五九零零零零