MATLAB Answers

scatter plot data that meet a condition

20 views (last 30 days)
Christina Geo
Christina Geo on 12 Sep 2021
Edited: Ive J on 12 Sep 2021
Hello, I have a matrix A(8,2000) and another one flag(8, 2000) which is logical.I would like to scatter plot data as follows: if an element of flag is 1 then i would like to represent the same element of table A as a red dote, while if is 0 as blue dot. The array x_axis has the values of x axis. Both axes i would like to be in logarithmic scale.
I tried this but i have the error
'Index exceeds matrix dimensions.'
Any idea about what i am doing wrong with dimensions?
x_axis=[1/6,1/3,1/2,1,2,3,6,12];
sz=8;
flag=(~flag==0);
for j=1:length(A)
scatter(x_axis,A(flag,j),sz,'r','filled');
set(gca, 'XScale', 'log','YScale','log')
hold on
scatter(x_axis,A(~flag,j),sz,'b','filled');
set(gca, 'XScale', 'log','YScale','log')
end

Accepted Answer

Ive J
Ive J on 12 Sep 2021
% some random values
A = randi([0, 100], 8, 2000);
F = randi([0 1], 8, 2000);
F = logical(F); % convert your flag to logical
figure;
x_axis = [1/6,1/3,1/2,1,2,3,6,12];
sz = 8;
x = repmat(x_axis.', 1, size(A, 2));
h = scatter(x(:), A(:), sz, 'filled');
col = repmat([0 0 1], numel(A), 1); % default color: blue
col(F(:), :) = repmat([1 0 0], sum(F(:)), 1); % set color of those with an available F to red(F(:, i), :) = repmat([1 0 0], sum(F(:, i)), 1); % set color of those with an available F to red
h.CData = col;
set(h.Parent, 'XScale', 'log', 'YScale', 'log')
  2 Comments
Ive J
Ive J on 12 Sep 2021
You can change them in the same way you would change their colors based on flag indices:
% some random values
A = randi([0, 100], 8, 5); % just to better see size of dots on the plot
F = randi([0 1], 8, 5);
F = logical(F); % convert your flag to logical
figure;
x_axis = [1/6,1/3,1/2,1,2,3,6,12];
sz = 8;
x = repmat(x_axis.', 1, size(A, 2));
h = scatter(x(:), A(:), sz, 'filled');
col = repmat([0 0 1], numel(A), 1); % default color: blue
col(F(:), :) = repmat([1 0 0], sum(F(:)), 1); % set color of those with an available F to red(F(:, i), :) = repmat([1 0 0], sum(F(:, i)), 1); % set color of those with an available F to red
h.CData = col;
% custom size based on flag indices
h.SizeData = sz.*ones(numel(A), 1); % default size of all dots
h.SizeData(F(:)) = 25; % set the size of those with an available F to 25 (those colored red).
set(h.Parent, 'XScale', 'log', 'YScale', 'log')
The line
col(F(:), :) = repmat([1 0 0], sum(F(:)), 1);
changes the default color (i.e. blue) for those with a flag value of 1 to red (RGB color code of [1 0 0]). repmat replicates a vector or matrix (in this case [1 0 0]) N times (in this case, sum(F)), and was used because we want to replace multiple rows of our color matrix (col has rows equal to numel(F) with 3 columns).
As a simple example:
mat = [1 0 0; 1 0 0; 1 0 0; 1 0 0]
mat =
1 0 0
1 0 0
1 0 0
1 0 0
idx = [false, true, false, true]; % indice to change: 2 and 4
mat(idx, :) = repmat([0 0 1], sum(idx), 1)
mat =
1 0 0
0 0 1
1 0 0
0 0 1

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by