Plotting vectors making chart

I have a 25x5 matrix, M, I want to obtain values from the third column which are >1.5, <0.5, and the remaining values of the third column. I would ideally like to plot them in different colors on the same graph. so far I have got this but I don't think its correct, N=M(:,3) if N>1.5 plot(N, 'r') hold on if N<0.5 plot(N, 'b') hold on plot(N, 'g') end
Thanks

 Respuesta aceptada

Voss
Voss el 17 de Nov. de 2022
Maybe something like this?
M = randn(100,5); % some random data for demo
N = M(:,3);
idx = find(N > 1.5);
plot(idx, N(idx), 'r.')
hold on
idx = find(N < 0.5);
plot(idx, N(idx), 'b.')
idx = find(N >= 0.5 & N <= 1.5);
plot(idx, N(idx), 'g.')

4 comentarios

M = randn(100,5); % some random data for demo
x = 1:size(M,1);
N = M(:,3);
idx_big = N > 1.5;
plot(x(idx_big), N(idx_big), 'r.')
hold on
idx_small = N < 0.5;
plot(x(idx_small), N(idx_small), 'b.')
idx_middle = ~idx_big & ~idx_small;
plot(x(idx_middle), N(idx_middle), 'g.')
Voss
Voss el 17 de Nov. de 2022
Editada: Voss el 18 de Nov. de 2022
@ac737: I'm going to share the message you sent me, clarifying the problem:
"... My matrix is [ redacted at OP's request - Voss ]
Each of the columns represent time(hrs) and the rows represent microbial activity. I want to produce a colour coded plot of the time course of activity of each gene so that any gene with:
activity at 3hrs (3rd column) > 1.5 is drawn in red
activity at 3hrs (3rd column) < 0.5 is drawn in blue
activity at 3 hrs (3rd column) otherwise drawn in green
I know I can plot a vector say a by
plot(a, 'r')
hold on
plot(a, 'b')
hold on
plot(a, 'g') etc.
Do I have to plot this in a loop 25 times to get the desired result?
My final figure should all start at 1, which makes me think I am supposed to plot for all microbes, sorry if this doesn't make sense, thank you for any help"
My response:
You can use a loop, and the code would not be much different from what you had in your original question:
M = [
%redacted
];
N = M(:,3);
figure()
hold on
for ii = 1:numel(N)
if N(ii) > 1.5
plot(M(ii,:), 'r')
elseif N(ii) < 0.5
plot(M(ii,:), 'b')
else
plot(M(ii,:), 'g')
end
end
Another way, using logical indexing instead of a loop, would be:
N = M(:,3);
figure()
hold on
idx_big = N > 1.5;
plot(M(idx_big,:).', 'r')
idx_small = N < 0.5;
plot(M(idx_small,:).', 'b')
idx_middle = ~idx_big & ~idx_small;
plot(M(idx_middle,:).', 'g')
Vc27
Vc27 el 17 de Nov. de 2022
Thank you!
Voss
Voss el 17 de Nov. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de Nov. de 2022

Editada:

el 18 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by