gpt4 book ai didi

arrays - 如何根据该值和一个新数组创建索引列表? MATLAB

转载 作者:行者123 更新时间:2023-12-04 09:17:31 26 4
gpt4 key购买 nike

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

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
但它显然不起作用......

最佳答案

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

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

关于arrays - 如何根据该值和一个新数组创建索引列表? MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63158145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com