Replace values in matrix with other values

后端 未结 4 2049
情歌与酒
情歌与酒 2020-12-10 13:16

I have a matrix with integers and I need to replace all appearances of 2 with -5. What is the most efficient way to do it? I made it the way below, but I am sure there is mo

4条回答
  •  醉酒成梦
    2020-12-10 13:39

    find is not needed in this case. Use logical indexing instead:

    a(a == 2) = -5
    

    In case of searching whether a matrix is equal to inf you should use

    a(isinf(a)) = -5

    The general case is:

    Mat(boolMask) = val

    where Mat is your matrix, boolMask is another matrix of logical values, and val is the assignment value

提交回复
热议问题