Change values in vectors

조회 수: 8 (최근 30일)
Io7
Io7 2023년 1월 21일
댓글: Vilém Frynta 2023년 1월 21일
I have a vector like: V=[1,2,3,4,5,6] and I want to substitute any values above 3 with the previous value in the vector which is “2”. So I want to have V=[1,2,2,2,2,2]. How to do this? Thank you.

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 21일
편집: Sulaymon Eshkabilov 2023년 1월 21일
V=[1,2,3,4,5,6]
V = 1×6
1 2 3 4 5 6
IDX = find(V>3);
V(IDX)=2
V = 1×6
1 2 3 2 2 2
%% ALT. Way:
V=[1,2,3,4,5,6]
V = 1×6
1 2 3 4 5 6
V(V>3)=2
V = 1×6
1 2 3 2 2 2

Vilém Frynta
Vilém Frynta 2023년 1월 21일
If you want to apply it to this vector, it's not complicated. But if you wanted to apply it to a more complex vector where the numbers don't go in ascending order, it woudle be bit more complicated.
I almost did it for you, but then I realized this sounds like a homework.
Please try, then we may help you. Try using find and indexing.
  댓글 수: 1
Vilém Frynta
Vilém Frynta 2023년 1월 21일
Well, nevermind, someone did it for you ÷)

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by