Substituting values of a matrix

冷暖自知 提交于 2019-12-24 10:49:30

问题


Say I have the following two matrices:

>> 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];

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.

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

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

Thanks.


回答1:


Try this:

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

Ans:

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


来源:https://stackoverflow.com/questions/15044667/substituting-values-of-a-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!