Dear All,
We have
a=1,2,3,4,5; (in a column 1)
b=-1000,10,0,20,-20; (in a column 2)
b_filtered=b(:,2)>=0;
plot (a,b_filtered);
How to plot these two values that after filtered they have two different dimensions?
Best,

 採用された回答

Chunru
Chunru 2022 年 7 月 13 日

1 投票

a = [1,2,3,4,5];
b = [-1000,10,0,20,-20];
idx = b>=0;
plot(a(idx), b(idx), 'o-');

3 件のコメント

Ara
Ara 2022 年 7 月 15 日
編集済み: Ara 2022 年 7 月 15 日
Thank you for your response.
What if, a and b are written like this a.a(:,1), a.a(:,2). Should be written like this?
plot(a.a (:,1)(idx), a.a (:,2)(idx), 'o-');
Chunru
Chunru 2022 年 7 月 16 日
plot(a.a(idx,1), a.a(idx,2), 'o-');
Ara
Ara 2022 年 7 月 16 日
Thank you.
your plot is different with the other answeres. I do not know which one is correct to adapt it to my code. Do you know?

サインインしてコメントする。

その他の回答 (3 件)

Mathieu NOE
Mathieu NOE 2022 年 7 月 13 日

1 投票

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 件のコメント

Ara
Ara 2022 年 7 月 13 日
Thank you for your reply.
The plot based on your code is different with the above answer? So, I am not sure, which one is the correct one.
Mathieu NOE
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');
Ara
Ara 2022 年 7 月 16 日
Hello Mathieu,
The plot you provided is different with the plot above provided by another expert who response to my question. I am confuse as I do not know which one is correct and how to modify my code based on which solution. Would you please tell me which one is correct?

サインインしてコメントする。

Ruchika P Barman
Ruchika P Barman 2022 年 7 月 13 日

1 投票

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");
Ara
Ara 2022 年 7 月 13 日

0 投票

Thank you Ruchika, for your response.
it means second column of my data should be greater than zero. I do not know which answer is correct as the figure for second answer isnot the same with yours. You and the first answer are the same output.
I have to modify it in a large set of data.

3 件のコメント

Ruchika P Barman
Ruchika P Barman 2022 年 7 月 13 日
編集済み: Ruchika P Barman 2022 年 7 月 13 日
Hello, @Ara.
Do you mean 'b' by 'second column of my data' or second column of 'b' which is a single element i.e 10? If it is the former, then the output from both the answers are correct.
Please let me know if I can help you further, thank you.
Ara
Ara 2022 年 7 月 14 日
Hello,
I meant second column of b, which is a column with many data.
Ara
Ara 2022 年 7 月 15 日
編集済み: Ara 2022 年 7 月 15 日
I have "data" file. When I open it in matlab that consist of 12 coumns and one of them is b. So, b is a column that contain a full range of data in a one column.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

Ara
2022 年 7 月 13 日

コメント済み:

Ara
2022 年 7 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by