delate a specific number in an array and the number that have the index corresponding to the this number in an other array

조회 수: 2 (최근 30일)
If I have two array A and B:
A=[0,0,3,4,5,6,7,8,9,0]
B=[10,9,8,7,6,5,4,3,2,1]
I want to delate the 0 in A and the number that have the index corresponding to the 0 in B obtaining:
A=[3,4,5,6,7,8,9]
B=[8,7,6,5,4,3,2]
To do that I would do a cycle with an if. I imagine is a common problem. Is there a simpler way to do it?
Thanks

채택된 답변

Jan
Jan 2022년 2월 22일
편집: Jan 2022년 2월 22일
A=[0,0,3,4,5,6,7,8,9,0];
B=[10,9,8,7,6,5,4,3,2,1];
match = (A == 0);
A(match) = []
B(match) = []
Or the other way around:
match = (A ~= 0);
A = A(match)
B = B(match)

추가 답변 (1개)

David Hill
David Hill 2022년 2월 22일
A=[0,0,3,4,5,6,7,8,9,0];
B=[10,9,8,7,6,5,4,3,2,1];
idx=A==0;
A(idx)=[];
B(idx)=[];

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by