繁体   English   中英

如何在矩阵的最后一列中移零

[英]How to shift zero in the last column of a matrix

我有一个像下面这样的矩阵 -

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

但我想把所有的0放在行的末尾,所以A应该像 -

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

我怎样才能做到这一点? Matlab专家请帮帮我。

你去吧 整个矩阵,没有循环,甚至适用于非连续的零:

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

At = A.'; %// It's easier to work with the transpose
[~, rows] = sort(At~=0,'descend'); %// This is the important part.
%// It sends the zeros to the end of each column
cols = repmat(1:size(At,2),size(At,1),1);
ind = sub2ind(size(At),rows(:),cols(:));
sol = repmat(NaN,size(At,1),size(At,2));
sol(:) = At(ind);
sol = sol.'; %'// undo transpose

像往常一样,对于不支持函数返回的~符号的Matlab版本,通过虚拟变量更改~ ,例如:

[nada, rows] = sort(At~=0,'descend'); %// This is the important part.

一个更通用的例子:

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

% Sort columns directly
[~,srtcol] = sort(A == 0,2);
% Sorted positions 
sz  = size(A);
pos = bsxfun(@plus, (srtcol-1)*sz(1), (1:sz(1))'); % or use sub2ind

结果

B = A(pos)
B =
     1     3     1     1     0
     1     1     1     2     0
     1     1     3     0     0

有很多方法可以做到这一点。 一个快速的方式可以很容易这样:

a = [1 2 3 4 0 5 7 0];

idx=(find(a==0));

idx =

     5     8

b=a;   % save a new copy of the vector
b(idx)=[];    % remove zero elements

b =

     1     2     3     4     5     7

c=[b zeros(size(idx))]

c =

     1     2     3     4     5     7     0     0

您也可以修改此代码。

如果你的零总是在一起,你可以使用circshift命令。 这会将数组中的值移动指定的位数,并将从边缘运行的值包装到另一侧。 看起来您需要为A每一行单独执行此操作,因此在上面的示例中,您可以尝试:

A(2,:) = circshift(A(2,:), [1 -1]);    % shift the second row one to the left with wrapping
A(3,:) = circshift(A(3,:), [1 -2]);    % shift the third row two to the left with wrapping

一般来说,如果你的零总是在A的行的前面,你可以尝试这样的:

for ii = 1:size(A,1)                              % iterate over rows in A
    numShift = numel(find(A(ii,:) == 0));         % assuming zeros at the front of the row, this is how many times we have to shift the row.
    A(ii,:) = circshift(A(ii,:), [1 -numShift]);  % shift it
end

试试这个(只是一个快速的黑客):

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

现在针对不支持~作为垃圾lhs-identifier的MATLAB版本进行了优化。

@ LuisMendo的答案是它的优雅,但我无法让它工作(也许是一个matlab版本的东西)。 以下(根据他的回答)为我工作:

Aaux = fliplr(reshape([1:numel(A)],size(A)));
Aaux(find(A==0))=0;
[Asort iso]=sort(Aaux.',1,'descend');
iso = iso + repmat([0:size(A,1)-1]*size(A,2),size(A,2),1);
A=A.';
A(iso).'

我也问了这个问题,得到了一个超级优雅的答案(以上答案都不一样): 在MATLAB中优化删除矩阵前导零

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM