How to shift zero in the last column of a matrix

前端 未结 7 1486
我在风中等你
我在风中等你 2020-12-11 10:58

I have one matrix like below-

A=[1 1 1 1 1;
   0 1 1 1 2;
   0 0 1 1 3]

But I want to place all the 0 at the end of the row, s

相关标签:
7条回答
  • 2020-12-11 11:27

    Try this (just a fast hack):

    for row_k = 1:size(A, 1)
       [A_sorted, A_sortmap] = sort(A(row_k, :) == 0, 'ascend');
    
       % update row in A:    
       A(row_k, :) = A(row_k, A_sortmap);
    end
    

    Now optimized for versions of MATLAB not supporting ~ as garbage lhs-identifier.

    0 讨论(0)
提交回复
热议问题