删除零然后垂直塌矩阵 [英] Removing zeros and then vertically collapse the matrix

查看:165
本文介绍了删除零然后垂直塌矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB,说我有一组方阵,比如说A,用跟踪(A)= 0如下:

In MATLAB, say I have a set of square matrices, say A, with trace(A)=0 as follows:

例如,

A = [0 1 2; 3 0 4; 5 6 0] 

我怎样才能去除零然后垂直塌矩阵成为如下:

How can I remove the zeros and then vertically collapse the matrix to become as follow:

A_reduced = [1 2; 3 4; 5 6]

更一般地,如果零所用的柱的任何地方出现什么(即,不一定在长对角线)?假设,当然,该零点的所有列的总数是相同的。

More generally, what if the zeroes can appear anywhere in the column (i.e., not necessarily at the long diagonal)? Assuming, of course, that the total number of zeros for all columns are the same.

该矩阵可以说是相当大的(数百点¯x数百尺寸)。因此,一个有效的方法将是AP preciated。

The matrix can be quite big (hundreds x hundreds in dimension). So, an efficient way will be appreciated.

推荐答案


  • 要融为一体preSS矩阵垂直(假设每列具有相同数量的零):

  • To compress the matrix vertically (assuming every column has the same number of zeros):

    A_reduced_v = reshape(nonzeros(A), nnz(A(:,1)), []);
    


  • 要融为一体preSS矩阵水平(假设每一行有相同数量的零):

  • To compress the matrix horizontally (assuming every row has the same number of zeros):

    A_reduced_h = reshape(nonzeros(A.'), nnz(A(1,:)), []).';
    


  • 这篇关于删除零然后垂直塌矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆