How to plot it?
3 次查看(过去 30 天)
显示 更早的评论
采纳的回答
Chunru
2022-7-13
a = [1,2,3,4,5];
b = [-1000,10,0,20,-20];
idx = b>=0;
plot(a(idx), b(idx), 'o-');
3 个评论
更多回答(3 个)
Mathieu NOE
2022-7-13
hello
simply replace the negative values of b by NaN so the vector size remains the same
a=[1;2;3;4;5];
b= [-10;10;0;20;-20];
b_filtered=b;
b_filtered(b<0) = NaN;
plot (a,b,a,b_filtered,'dr');
3 个评论
Mathieu NOE
2022-7-13
hello
both answers are correct and will display the exact same plot
I wanted to have both b and b_filtered the same size as a , because it seemed to me that you wanted it this way.
the other options as suggested by @Chunru works also , and if you want to have both b and b_filtered displayed , you can slightly modify his code :
it's up to you to pick the one you prefer
a = [1,2,3,4,5];
b = [-10,10,0,20,-20];
idx = b>=0;
plot(a,b,a(idx), b(idx), 'or');
Ruchika P Barman
2022-7-13
It is my understanding that you are trying to plot a and b_filtered. I believe b_filtered is supposed to contain the non-negative elements of b, please correct me if I'm wrong on this, I did not understand what exactly is (b(:,2)>=0) supposed to do? If I'm not wrong, we can retain the elements of a at the indices where b is not negative and plot a and b accordingly.
a=[1;2;3;4;5];
b= [-10;10;0;20;-20];
b(b<0)=NaN;
a(b<0)=NaN;
b_filtered=b;
p=plot (a,b_filtered,"red");
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!