我有一组数据。为简单起见,我们称其为 4 x 3 矩阵。假设我想在第 2 列中找到值为 5 的数据点。然后,我想获取第 2 列中包含值 5 的所有行并将其放入自己的数组中。我的数据比下面显示的数据大得多,所以我不想通过眼睛查看每一行数据并识别所有 5。
% My idea of the code:
data = [1 2 3 4; 5 5 5 6; 6 4 5 6]
if data(:,2) == 5
% This is the part I can't figure out
end
我们将 finaldata 称为数组,其中将存储带有 5 的数据。我该怎么做?
最佳答案
你应该使用 logical indexing :
all_fives_rows = data(data(:, 2) == 5, :)
关于arrays - 如何根据列中的特定值从数组中挑选出行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248440/