Converting sequence to binary based on the value exceeding condition

조회 수: 4 (최근 30일)
William Taylor
William Taylor 2020년 12월 27일
댓글: Image Analyst 2020년 12월 27일
Say I have a sequence x = [2 4 3 7 5 4 3], and I want to convert the sequence to 1s and 0s based on if the value at each index position is greater than 5. So the output of the function would give y = [0 0 0 1 1 0 0]...what would this function look like?
I have attempted this from the below, could you also explain if this is the correct path?
Many thanks for your help
for i=length(x)
if x(i:end) > 5
y(i) = 1;
else
y(i) = 0;
end

답변 (1개)

Image Analyst
Image Analyst 2020년 12월 27일
One line of code is all it takes:
y = x > 5
  댓글 수: 1
Image Analyst
Image Analyst 2020년 12월 27일
or
y = x >= 5
depending on what you want. (Your question shows it both ways and is ambiguous.)

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by