Replace values in matrix with other values

后端 未结 4 2045
情歌与酒
情歌与酒 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:30

    The Martin B's method is good if you are changing values in vector. However, to use it in matrix you need to get linear indices.

    The easiest solution I found is to use changem function. Very easy to use:

    mapout = changem(Z,newcode,oldcode) In your case: newA = changem(a, 5, -2)

    More info: http://www.mathworks.com/help/map/ref/changem.html

提交回复
热议问题