필터 지우기
필터 지우기

how to delete rows from matrix

조회 수: 1 (최근 30일)
m. muner
m. muner 2016년 5월 24일
댓글: Anoire BEN JDIDIA 2016년 10월 17일
hello i have 53*3 matrix but include rows that three column of the row are zeros i want to delete the entire row i tried this but since every delete shift the location of the other rows i had problem with the for loop so what can i do to overcome this
[c u]=size(test_ary); %dimensions
for i=1:c
for b=1:u
if(test_ary(i,1)==(o)));
test_ary(i,:)=[];
end
end
end

채택된 답변

James Tursa
James Tursa 2016년 5월 24일
편집: James Tursa 2016년 5월 24일
Replace your for-loop with this vectorized code:
x = all(test_ary==0,2); % Which rows are all 0's
test_ary(x,:) = []; % Delete those rows
  댓글 수: 1
m. muner
m. muner 2016년 5월 26일
not working its give the same matrix

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jos (10584)
Jos (10584) 2016년 5월 26일
Using the statement "test_ary(i,:) = []" you will change the size of it, which will cause problems!
tf = all(test_ary==0,2) % true for rows with only 0's
test_ary(tf,:) = [] % remove those rows using logical indexing

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 24일
out=test_ary(~ismember(test_ary,[0 0 0],'rows'),:)
  댓글 수: 2
m. muner
m. muner 2016년 5월 26일
just create new matrix called out which is same as the original test_ary
Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 26일
No, this is not true

댓글을 달려면 로그인하십시오.


Anoire BEN JDIDIA
Anoire BEN JDIDIA 2016년 10월 14일
I have a big matrix 599794x2 i want to delete rows which contains values which repeats for exemple if A=[1,1;2,1;3,1;4,1;5,2;6,2;7,2]; i want to have A=[1,1;5,2]
  댓글 수: 2
James Tursa
James Tursa 2016년 10월 14일
In the future open up a new Question for this rather than piggyback on an existing Question. But I will answer this here this time:
A = A(logical([1;diff(A(:,2))]),:);
Anoire BEN JDIDIA
Anoire BEN JDIDIA 2016년 10월 17일
Hi, Thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by