MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   将指定索引设置为零 (https://www.labfans.com/bbs/showthread.php?t=26561)

poster 2019-12-14 20:13

将指定索引设置为零
 
我有两个大小相同的矩阵( x1和x2 )。我想在x1使用等于零的元素将x2元素置于零。

我现在得到的无效解决方案如下:

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

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



[B]回答:[/B]

尝试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)-1x2 = 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)= 0x1 = 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

[url=https://stackoverflow.com/questions/5258720]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:03

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.