【问题标题】:How do a create a list of indexes based on the value and a new array from that? MATLAB如何根据值和新数组创建索引列表? MATLAB
【发布时间】:2020-11-19 07:29:07
【问题描述】:

我想消除我的数据集中第三行包含零值的所有列。

举个例子:

original_data = [1 2 3 4 5; 1 2 3 4 5; 0 0 0 1 2] 

对于前三列(第三行为零),我想创建一个新数组,其中删除第三行为零的列以获得结果:

new_data = [ 4 5;  4 5; 1 2] 

我还想要一个原始数组中非零值的列索引数组。

例如:

original_indices = [4, 5]

我试过了:

dados_teste = dados_out_15;
dados_p6 = [];
[m,n] = size(dados_teste)
for i = 1:n
    
    if dados_teste(3:i) == 0;
        dados_p6 = dados_teste(:,i)
    else
        dados_p6 = dados_teste(:,n)
    end
end

但它显然不起作用......

【问题讨论】:

  • 您应该在尝试自己解决此问题后提供 MCVE。 SO 不能替代基础研究。
  • 你好疯狂,对不起。我用我尝试过的代码更新了我的问题。
  • 使用随机数做一个小例子,以便重现。查看randi 左右。显示您的输入和预期输出。通过与预期输出的比较来解释你的代码做了什么。请阅读stackoverflow.com/help/how-to-ask 以获得进一步说明。
  • 我刚刚做到了。非常感谢!

标签: arrays matlab indexing


【解决方案1】:

我会应用 find() 函数来查找所有非零索引,然后应用矩阵索引来生成一个新数组,该数组仅包含与第三行。

Sample_Array = [20 30 40 50; 30 20 70 90; 0 2 1 2];

%Grabbing the third row of the matrix%
Third_Row = Sample_Array(3,:);

%Finding all the non-zero indices%
[Non_Zero_Indices] = find(Third_Row);

%Using matrix indices to generate a new array based on the non-zero
%indicies%
New_Matrix = Sample_Array(:,Non_Zero_Indices);

%Printing matrices% 
Sample_Array

New_Matrix

Non_Zero_Indices

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多