替换矩阵的值 [英] Substituting values of a matrix

查看:94
本文介绍了替换矩阵的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下两个矩阵:

>> x = [1 4 3; 6 4 3; 6 9 3; 2 4 3; 5 4 0; 5 3 1; 6 4 7];
>> y = [0 0 1; 1 1 0; 1 1 0; 0 1 1; 0.2 0.8 0.54; 1 1 1; 0 0 0];

在其中您可以将x视为某些图像,并将y每个x元素的隶属度视为某个感兴趣区域.

Where you can think of x as some image, and y the degree of membership of of each element of x to some region of interest.

假设我将x中具有隶属度= 1的那些元素设置为1,将其他元素设置为0,如下所示:

Say I set those elements in x that have degree of membership = 1 to 1 and the other elements to 0 as follows:

x = zeros(size(y));
x(y==1) = 1;

在这种情况下,我将获得以下输出:

In which case I will have the following output:

     0     0     1
     1     1     0
     1     1     0
     0     1     1
     0     0     0
     1     1     1
     0     0     0

现在,对于0的元素,如何在相应位置将其值替换为y的值?

Now, for the elements of 0, how can I substitute their values with the value of y in the corresponding location?

谢谢.

推荐答案

尝试一下:

x(x==0)=y(x==0);

回答:

x =

     0         0    1.0000
1.0000    1.0000         0
1.0000    1.0000         0
     0    1.0000    1.0000
0.2000    0.8000    0.5400
1.0000    1.0000    1.0000
     0         0         0

这篇关于替换矩阵的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆