What is the inverse of find() function in matlab

后端 未结 2 453
长情又很酷
长情又很酷 2021-01-20 02:37

In Matlab I can find all non zero entries in a vector like this:

>> v = [0 1 0 0 1]

v =

     0     1     0     0     1

>> indices = find(v)

i         


        
2条回答
  •  自闭症患者
    2021-01-20 03:15

    i used to use sparse and full but now i switched to accumarray, because it has simpler format.

    in your example:

       v=accumarray([2;5],1)'
    

    additionally, you can also cycle values (i think), according to the first matlab example:

    Create a 5-by-1 vector, summing values for repeated 1-D subscripts:
       subs = [1; 2; 4; 2; 4];
       A = accumarray(subs, 101:105)
    

    of course, according to its name, this function is built to do fancier things.

提交回复
热议问题