我有一个矩阵的形式:
m = 1, 0, 10, 20, 30, 40, 50;
2, 1, 11, 20, 30, 40, 50;
3, 0, 12, 20, 30, 40, 50;
4, 1, 12, 21, 30, 40, 50;对于给定的列索引(例如3)和行索引(例如1),我希望筛选出该行中该列右侧具有相同值的所有行。使用上面的m、columnIndex = 3和rowIndex =1(用星号标记)的示例:
**
f(m, 3) = * 1, 0, 10, 20, 30, 40, 50; % [20, 30, 40, 50] matches itself, include
2, 1, 11, 20, 30, 40, 50; % [20, 30, 40, 50] matches the subvector in row 1, include
3, 0, 12, 20, 30, 40, 50; % [20, 30, 40, 50] matches the subvector in row 1, include
4, 1, 12, 21, 30, 40, 50; % [21, 30, 40, 50] does NOT match the subvector in row 1, filter this out如何实现此行为?我已经尝试过了,但是我得到了一个尺寸不匹配的错误。
key = data( rowIndex, columnIndex:end );
filteredData = ( data( :, columnIndex:end ) == key );发布于 2013-06-20 03:05:46
在bsxfun()中为使用==保存的内容编制索引
r = 3;
c = 2;
idx = all(bsxfun(@eq, m(:,c:end),m(r,c:end)),2);
m(idx,:)发布于 2013-06-20 02:49:13
我认为您正在考虑使用isequal运算符documented here。
isequal(m(1,columnIndex:end),key)这是一个低效的线条:-)
cellfun(@(x) isequal(key,x),mat2cell(m(:,columnIndex:end),ones(1,size(m,2)-columnIndex+1)))事情是这样发展的:
将矩阵更改为我们感兴趣的子向量的单元格数组:在每个单元格元素上运行的mat2cell(m(:,columnIndex:end),ones(1,size(m,2)-columnIndex+1))
@(x) isequal(key,x)
cellfun(
我的答案是以上面的m为例:
cellfun(@(x) isequal(key,x),mat2cell(m(:,columnIndex:end),ones(1,size(m,2)-columnIndex+1)))
ans =
1
1
1
0哈!
https://stackoverflow.com/questions/17198680
复制相似问题