How to delete zeros from matrix in MATLAB?

前端 未结 2 467
你的背包
你的背包 2021-01-20 22:56

Here is my problem:

I have a nxn matrix in matlab. I want to delete all the zeros of this matrix and put the rows of it in vectors. For n=4

2条回答
  •  广开言路
    2021-01-20 23:25

    Convert to a cell array such that you have a cell for each row and then use nonzeros for each cell, that deletes zeros and finally store them into separate variables.

    Code

    nzv =cellfun(@(x) nonzeros(x),mat2cell(A,ones(1,size(A,1)),size(A,2)),'uni',0)
    [v1,v2,v3,v4] = nzv{:}
    

提交回复
热议问题