简体   繁体   English

在MATLAB中的一组特定行上使用逻辑索引

[英]Using logical indexing on a specific set of rows in MATLAB

I want to return the rows in a cell array which meet a condition that is repeated every few (but variable) lines. 我想要返回满足每隔几行(但可变行)重复一次的条件的单元格数组中的行。 For example if my data is x,y,z coordinates split up into i slices where each slice{i} is a specific z I could have something that looks like this. 例如,如果我的数据是x,y,z坐标,则分解为i个切片,其中每个slice {i}是一个特定的z,我可能会有类似这样的内容。

1,3,10
1,4,10
1,5,10
2,3,10
2,4,10
3,1,10

For each x I want to return the rows containing the max and min y values. 对于每个x,我想返回包含max和min y值的行。 So in this case I want rows 1, 3, 4, 5, 6. 所以在这种情况下,我想要第1、3、4、5、6行

The code I have now looks like this 我现在拥有的代码如下所示

idx = slice{i}(start:finish,2) == miny | slice{i}(start:finish,2) == maxy;
return = slice{i}(idx, :);

But the line slice{i}(idx, :) looks through the entire array from the beginning. 但是行slice {i}(idx,:)从头开始遍历整个数组。 I want to restrict this line to a certain subset. 我想将此行限制为某个子集。

Something like 就像是

slice{i}(idx, start:finish)

but this doesn't work. 但这不起作用。

Am I missing some syntax or do I need to approach the routine in a different way? 我是否缺少某些语法,还是需要以其他方式处理例程? (I know I haven't provided enough information for help in changing approach but I am assuming there is some way to do this by restricting the row indicies) (我知道我没有提供足够的信息来帮助您更改方法,但是我认为可以通过限制行索引来实现此目的)

edit: 编辑:

I found a workaround by creating 我通过创建找到了解决方法

dummy = slice{i}(start:finish, :);    

and then just returning over the dummy matrix. 然后只返回虚拟矩阵。

See if this works for you - 看看这是否适合您-

%// Convert slice to a nuemric array
slicenum = cell2mat(slice)

%// Create the offset arary to be added with the local max-min indices to
%// get the global(actual) row indices for idexing into slicenum or slice
offset1 = [0 ; find(diff(slicenum(:,1)))]

[~,~,unqX] = unique(slicenum(:,1))

%// Get local max and min row indices
max_row_ind = accumarray(unqX,slicenum(:,2),[],@max_ind) + offset1
min_row_ind = accumarray(unqX,slicenum(:,2),[],@min_ind) + offset1

%// Get global row indices and use them to index into slicenum for the output
max_y_slice = slicenum(max_row_ind,:)
min_y_slice = slicenum(min_row_ind,:)

Associated functions - 相关功能-

function ix = max_ind(array1)
[~,ix] = max(array1);
return;

function ix = min_ind(array1)
[~,ix] = min(array1);
return;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM