if statement for a matrix
2 次查看(过去 30 天)
显示 更早的评论
Hi my x-axis is a 1x72 matrix having values from 1 to 72.
y axis values are again 1x72 matrix having data values.
I want the "if loop" to go on for only those x values which are between 10 and 25, and plot x vs y for only those selected values (y axis data values corresponding to the values between 10 to 25). Could you please evaluate my code.
for i = 1:length(x)
if (x>10) && (x<25)
plot(x,y)
end
end
0 个评论
采纳的回答
Star Strider
2019-3-11
Use ‘logical indexing’:
x = 1 : 72;
y = rand(1, 72);
mv = (x>10) & (x<25);
figure
plot(x(mv), y(mv))
grid
xlim([min(x) max(x)])
Experiment to get the result you want.
0 个评论
更多回答(2 个)
Alex Mcaulley
2019-3-11
You don't need the loop, just using logical indexing:
plot(x(x>10&x<25),y(x>10&x<25))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!