select rows satisfying a particular condition

조회 수: 3 (최근 30일)
Elysi Cochin
Elysi Cochin 2019년 4월 25일
편집: Alex Mcaulley 2019년 4월 25일
having a matrix of dim 5 x 10
if i give start_index = 1, and end_index = 3, i wanted to get rows starting with value of start_index and ending with end_index excluding zeros.
eg: rows 4 and 5
cell_matrix = {[1 2 3]
[1 3] }
saved to a cell matrix

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 4월 25일
% Let A - your array.
[ii,jj,v] = find(A);
z = [ii,jj,v];
z = sortrows(z,[1,2]);
out = accumarray(z(:,1),z(:,3),[],@(x){fun(x,1,3)});
function out = fun(x,b,e)
x = x(:)';
out = [];
if isequal(x([1,end]) , [b,e]) && all(diff(x(:)) > 0)
out = x;
end
end

추가 답변 (1개)

Alex Mcaulley
Alex Mcaulley 2019년 4월 25일
편집: Alex Mcaulley 2019년 4월 25일
a = randi([0,2],5,10);
start_index = 1;
end_index = 3;
b = mat2cell(a(start_index:end_index,:),ones(1,end_index-start_index+1),size(a,2));
b = cellfun(@(x) myfun(x),b,'UniformOutput',false);
function x = myfun(x)
x(x==0) = [];
end

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by