How to change colour of binary vector

1 回表示 (過去 30 日間)
Emily Cooper
Emily Cooper 2019 年 7 月 7 日
編集済み: Star Strider 2019 年 7 月 8 日
Hi,
I have a dataset with vectors x,y,a,b (all of the same length, single column each)
I need to make a 2D plot of x,y but only dependent on whether the point occurs in both a and b (so eliminating data points that only occur in one or the other). I have managed to code for this using scatter3 (and then just rotating the figure to show a 2D perspective) like this:
figure(1)
a(a == 0) = NaN;
index = ( isnan(b) & ~isnan(a));
index1 = double(index)
index1(index1 == 1) = NaN;
scatter3(x,y,index1,pointsize,a,'filled')
hold on;
a1(a1 == 0) = NaN;
indexa = ( isnan(b) & ~isnan(a1));
indexa1 = double(indexa)
indexa1(indexa1 == 1) = NaN;
scatter3(x,y,indexa1,pointsize,a1,'x')
hold on;
etc
My code isn't quite right because the only way to plot what I want is to have the colour set as a - otherwise it plots all data points from x and y regardless of index1, and i'm not sure why, so I'd be grateful for any suggestions for how to code the whole thing differently!
Eventually I need to plot lots of different vectors in the same way on the same graph. But I need each set to be coloured differently (just having different marker types doesn't make the image clear enough). - and as I have to set a as the colour, i cant figure this out.
Thanks :)
  2 件のコメント
the cyclist
the cyclist 2019 年 7 月 7 日
Can you please clarify what you mean by the phrase "but only dependent on whether the point occurs in both a and b"?
Do you mean, for example,
  • a and b have to have the same value?
  • a and b can be different, but both must be non-NaN value?
  • a and b can be different, but both must be non-zero?
Or something else?
Emily Cooper
Emily Cooper 2019 年 7 月 8 日
Hi,
a is a binary vector and b is a vector where values are either something like 234967 or are NaN. To be included in the figure, a must have a value of 1 and b must have a numerical value (not NaN).
So I tried to convert 0s in a to NaN and then exclude NaN from the data.
(If the value in a is 0 but corresponding (same row) value in b is numerical, the data point is not included)

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

採用された回答

Star Strider
Star Strider 2019 年 7 月 7 日
編集済み: Star Strider 2019 年 7 月 8 日
We don’t have your data so can’t run your code.
Try this:
x = 0:19;
y = sin(2*pi*x/19);
a = randi(5, 20, 1);
b = randi(5, 20, 1);
Lidx = a == b;
Q1 = x(Lidx);
Q2 = y(Lidx);
Q3 = find(Lidx);
figure
scatter(x(Lidx)', y(Lidx)', 20, find(Lidx), 'filled')
EDIT —
a is a binary vector and b is a vector where values are either something like 234967 or are NaN. To be included in the figure, a must have a value of 1 and b must have a numerical value (not NaN).
Assuming that by ‘binary’ you mean ‘logical’, try this:
index = a & ~isnan(b);
If ‘a’ is numeric (not logical), you can easily convert it to being logical with:
a = a == 1;
then:
index = a & ~isnan(b);
Both ‘a’ and ‘b’ must have the same row and column sizes.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by