How to find specific entry values in a vector?

I would like to consider only the 3's between the 1's and 2's.
x = [1, 1, 1, 3, 3 ,3 ,2 ,2, 3, 3, 1, 1, 1 , 3, 3, 2 ,3,...];
Desired result: r = [0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 ,3 ,3 , 0 ,....]
for i = 1:length(x)-1
if x(i)==1 && x(i+1) == 3
while ...?

답변 (1개)

KSSV
KSSV 2021년 5월 20일
x = [1, 1, 1, 3, 3 ,3 ,2 ,2, 3, 3, 1, 1, 1 , 3, 3, 2 ,3];
x(x~=3) = 0
x = 1×17
0 0 0 3 3 3 0 0 3 3 0 0 0 3 3 0 3

댓글 수: 1

LS
LS 2021년 5월 20일
Thanks, but it's not what I meant. I would like to get these values (every group of 3's between 1 and 2)

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

LS
2021년 5월 20일

댓글:

LS
2021년 5월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by