How to remove zeros from a vector and remove values from those indices in a corresponding vector?

조회 수: 1 (최근 30일)
I have 3 row vectors that are quite long, but for example they are:
D=[0 0 100 0 250 0 300]
H=[4 2 0 5 0 0 1]
T=[7 9 12 4 9 4 12]
I want to remove the values from H and T that correspond to zero in D, but I want to leave the zeros in H and T.
For example, the above vectors would become:
D=[100 250 300]
H=[0 0 1]
T=[12 9 12]
How to do? Thank you in advance.

채택된 답변

Voss
Voss 2023년 8월 14일
D=[0 0 100 0 250 0 300];
H=[4 2 0 5 0 0 1];
T=[7 9 12 4 9 4 12];
D,H,T
D = 1×7
0 0 100 0 250 0 300
H = 1×7
4 2 0 5 0 0 1
T = 1×7
7 9 12 4 9 4 12
idx = D == 0;
D(idx) = [];
H(idx) = [];
T(idx) = [];
D,H,T
D = 1×3
100 250 300
H = 1×3
0 0 1
T = 1×3
12 9 12
  댓글 수: 2
Image Analyst
Image Analyst 2023년 8월 14일
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by