Converting sequence to binary based on the value exceeding condition
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Image Analyst
2020-12-27
One line of code is all it takes:
y = x > 5
1 个评论
Image Analyst
2020-12-27
or
y = x >= 5
depending on what you want. (Your question shows it both ways and is ambiguous.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!