删除矩阵中的零并减小大小

时间:2018-11-03 18:48:20

标签: arrays matlab matrix

如何删除矩阵中的零并减小其大小。

现在这是我的矩阵:enter image description here

我希望它像这样:

745,4.8
740,3
747,11
742,13
746,7.2
741,12.2

Matlab矩阵:https://drive.google.com/file/d/1P5h6lhs0O2fxFcxHabAO4hU84UqAZD1m/view?usp=sharing

有人可以给我一些如何做的提示/指针吗?

load('matlab_matrix.mat')
L=length(num);
num2=num(4:72,:);
L2=length(num2);
L3=length(num2(1,:));

for i=1:L2;
    for j=1:L3;
        if isnan(num2(i,j)) ~= 1;
            A(i,j)=num2(i,j);
        end     
    end    
end

2 个答案:

答案 0 :(得分:4)

Let's define your data:

x = [751 0 0 0
     750 0 0 0
     749 0 0 0
     748 0 0 0
     747 0 11 0
     746 0 0 7.2
     745 4.8 0 0
     744 0 0 0
     743 0 0 0
     742 0 13 0
     741 0 0 12.2
     740 3 0 0
     739 0 0 0];

Then:

ind = find(x(:,2:end));
result = [x(mod(ind-1,size(x,1))+1) x(ind+size(x,1))];

This works even if each row has a different number of nonzeros.

To understand how it works, you may want to take a look at Linear indexing

答案 1 :(得分:0)

似乎每行中的零个数不相等,因此仅删除零点并不能解决问题。您可以使用稀疏矩阵来减小大小:

tensorflow/core/distributed_runtime/master.cc:267] CreateSession still waiting for response from worker: /job:ps/replica:0/task:0

如果矩阵包含NaN,则可以使用:

result = sparse(num);