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

 채택된 답변

Voss
Voss 2022년 11월 17일

0 개 추천

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

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 2022년 11월 17일
편집: Voss 2022년 11월 18일
@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 2022년 11월 17일
Thank you!
Voss
Voss 2022년 11월 17일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2022년 11월 17일

편집:

2022년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by