Changing color of a certain cell in a matrix after breaking a for-loop

1 次查看(过去 30 天)
Hi! I have a question regarding changing a certain cell when breaking a for loop. In my code, I have criterias for the top and bottom row of cells which if fulfilled the loop will be broken. However, when the criteria is achieved I would like to change the cell that made the break of the loop a certain color (like red). Is this possible, and if how do I write this? All the help is appreciated!
Clarification: If the 1x3 cell of the 10x10 matrix causes the loop to break, I want to change this certain cell in the color red.
Thanks in beforehand!
clc
clear all
I = 0.2; % <-- Startkoncentrationen
a10=0.2; % <-- Renande i varje iteration
b0 = I; ep0 = 0; c0=0; a10=0.2; b(1) = b0; ep1(1)= ep0;c1(1) = c0;ep2(1)=ep0;c2(1)=c0;
tMax=10000; % <-- Tidsintervall (enhet?)
dt=0.1; timeVector = 0:dt:tMax;
A = [];
for i = 1:9:900
ra12_1(i) = rand; ia12_1 = 0.1;% <-- Probability att a12_1 inträffar
if ra12_1(i)>=ia12_1
a12_1(i)=0.1; % <-- 'Mängden' av flödet för a12_1
else
a12_1(i) = 0;
end
ra12_2(i) = rand; ia12_2 = 0.1;% <-- Probability att a12_2 inträffar
if ra12_2(i)>=ia12_2
a12_2(i)=0.1; % <-- 'Mängden' av flödet för a12_2
else
a12_2(i) = 0;
end
ra23_1(i) = rand; ia23_1=0.1;% <-- Probability att a23_1 inträffar
if ra23_1(i) >= ia23_1
a23_1(i) = 0.1; % <-- 'Mängden' av flödet för a23_1
else
a23_1(i) = 0;
end
ra23_2(i) = rand; ia23_2=0.1;% <-- Probability att a23_2 inträffar
if ra23_2(i) >= ia23_2
a23_2(i) = 0.1; % <-- 'Mängden' av flödet för a23_2
else
a23_2(i) = 0;
end
ra21_1(i) = rand; ia21_1=0.1;% <-- Probability att a21_1 inträffar
if ra21_1(i) >= ia21_1
a21_1(i) = 0.1; % <-- 'Mängden' av flödet för a21_1
else
a21_1(i) = 0;
end
ra21_2(i) = rand; ia21_2=0.1;% <-- Probability att a21_2 inträffar
if ra21_2(i) >= ia21_2
a21_2(i) = 0.1; % <-- 'Mängden' av flödet för a21_2
else
a21_2(i) = 0;
end
c1(1) = c0;
ep1(1) = ep0;
b(1) = b0;
ep2(1)= ep0;
c2(1) = c0;
c1(i+1)=c1(i)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+1)=ep1(i)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+1)=b(i)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+1)=ep2(i)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+1)=c2(i)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+2)=c1(i+1)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+2)=ep1(i+1)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+2)=b(i+1)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+2)=ep2(i+1)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+2)=c2(i+1)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+3)=c1(i+2)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+3)=ep1(i+2)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+3)=b(i+2)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+3)=ep2(i+2)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+3)=c2(i+2)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+4)=c1(i+3)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+4)=ep1(i+3)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+4)=b(i+3)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+4)=ep2(i+3)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+4)=c2(i+3)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+5)=c1(i+4)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+5)=ep1(i+4)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+5)=b(i+4)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+5)=ep2(i+4)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+5)=c2(i+4)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+6)=c1(i+5)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+6)=ep1(i+5)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+6)=b(i+5)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+6)=ep2(i+5)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+6)=c2(i+5)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+7)=c1(i+6)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+7)=ep1(i+6)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+7)=b(i+6)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+7)=ep2(i+6)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+7)=c2(i+6)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+8)=c1(i+7)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+8)=ep1(i+7)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+8)=b(i+7)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+8)=ep2(i+7)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+8)=c2(i+7)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
c1(i+9)=c1(i+8)+dt*liver1_cont(timeVector(i),a23_1(i),ep1(i));
ep1(i+9)=ep1(i+8)+dt*endo1_cont(timeVector(i),a12_1(i),ep1(i),a21_1(i),b(i),a23_1(i));
b(i+9)=b(i+8)+dt*blood_cont(timeVector(i), a10, a12_1(i), a12_2(i), b(i), a21_1(i), a21_2(i), ep1(i), ep2(i), I);
ep2(i+9)=ep2(i+8)+dt*endo2_cont(timeVector(i),a12_2(i),ep2(i),a21_2(i),b(i),a23_2(i));
c2(i+9)=c2(i+8)+dt*liver2_cont(timeVector(i),a23_2(i),ep2(i));
if c1(i)>0.9 || c1(i+1)>0.9 || c1(i+2)>0.9 || c1(i+3)>0.9 || c1(i+4)>0.9 || c1(i+5)>0.9 || c1(i+6)>0.9 || c1(i+7)>0.9 || c1(i+8)>0.9 || c1(i+9)>0.9%if sum av c1/c2 är högre än ett visst värde, break --> cancer, if sum(c1,'all') >5, break
break
elseif c2(i)>0.9 || c2(i+1)>0.9 || c2(i+2)>0.9 || c2(i+3)>0.9 || c2(i+4)>0.9 || c2(i+5)>0.9 || c2(i+6)>0.9 || c2(i+7)>0.9 || c2(i+8)>0.9 || c2(i+9)>0.9
break
end
figure(1)
A = [c1(i), c1(i+1), c1(i+2), c1(i+3), c1(i+4), c1(i+5), c1(i+6), c1(i+7), c1(i+8), c1(i+9); ep1(i), ep1(i+1), ep1(i+2), ep1(i+3), ep1(i+4), ep1(i+5), ep1(i+6), ep1(i+7), ep1(i+8), ep1(i+9); b(i), b(i+1), b(i+2), b(i+3), b(i+4), b(i+5), b(i+6), b(i+7), b(i+8), b(i+9); ep2(i), ep2(i+1), ep2(i+2), ep2(i+3), ep2(i+4), ep2(i+5), ep2(i+6), ep2(i+7), ep2(i+8), ep2(i+9); c2(i), c2(i+1), c2(i+2), c2(i+3), c2(i+4), c2(i+5), c2(i+6), c2(i+7), c2(i+8), c2(i+9)]
imagesc(A)
colormap summer
colorbar
pause(.2)
end
for j = 1:i+8
timeVector_plot(j+1) = [dt*j:dt*j(end)];
end
conc_liver1 = [c1];
conc_endo1 = [ep1];
conc_blood = [b];
conc_endo2 = [ep2];
conc_liver2 = [c2];
figure(2)
plot(timeVector_plot, conc_blood, 'r')
hold on
plot(timeVector_plot, conc_endo1, 'g.')
hold on
plot(timeVector_plot, conc_liver1, 'b.')
hold on
plot(timeVector_plot, conc_liver2, 'c')
hold on
plot(timeVector_plot, conc_endo2, 'g')
xlabel('Time')
ylabel('Concentration of agent'); legend('[Blood]', '[Upper endothelial]', '[Upper liver]', '[Lower endothelial]', '[Lower liver]')
function dcdt = liver1_cont(t, a23_1, ep1)
dcdt = a23_1.*ep1;
end
function dcdt = liver2_cont(t, a23_2, ep2)
dcdt = a23_2.*ep2;
end
function depdt = endo1_cont(t, a12_1, ep1, a21_1, b, a23_1)
depdt = a12_1*b-(a21_1+a23_1)*ep1;
end
function depdt = endo2_cont(t, a12_2, ep2, a21_2, b, a23_2)
depdt = a12_2*b-(a21_2+a23_2)*ep2;
end
function dbdt = blood_cont(t, a10, a12_1, a12_2, b, a21_1, a21_2, ep1, ep2, I)
dbdt = -(a10+a12_2+a12_1).*b+a21_2.*ep2+a21_1.*ep1 +I;
end
  4 个评论
Ive J
Ive J 2020-12-28
S, you can start by sharing a piece of code that actually works, e.g. you haven't identified what liver1_cont is, and further show exactly what you mean by certain cell. Cell is a MATLAB data type, so color property doesn't make any sense for a cell unless you intend to display (in command window) or visualize on figure.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2020-12-28
Before the break command in your if statement, include code for changing the color. Since you are using imagesc, this means actually changing the value of the underlying data rather than simply assigning a color. You will likely need to change your colormap from summer to something that includes red in it as well.
  4 个评论
Cris LaPierre
Cris LaPierre 2020-12-28
One last thing. The resolution is going to be defined by the number of steps in your colormap. Since anything about 0.9 is to be red, another way to do it might be this.
myCmap = [summer(900);[1 0 0]];
colormap(myCmap)
colorbar
caxis([0 0.9])
Here, any value >0.9 will be displayed at the color of 0.9. The colormap is created with 900 steps between 0 and 0.9, which gives you a resolution of around 0.001. Only the very last value (0.9) is red, so it is barely visible on the colorbar (not sure if that is desired).
Anyway, sounds like you know where to go from here to get the result you need.

请先登录,再进行评论。

更多回答(1 个)

Ive J
Ive J 2020-12-28
编辑:Ive J 2020-12-28
You could overlay red rectangles on those cells passing the predefined cutoff (if you wanna stick with the summer colormap).
if any(c1(i:i+9) > 0.9)%if sum av c1/c2 är högre än ett visst värde, break --> cancer, if sum(c1,'all') >5, break
% locate those above 0.9 cutoff
c1Idx = find(c1(i:i+9) > 0.9);
for j = 1:numel(c1Idx)
rectangle('Position', [c1Idx(j) - 0.5, 0.5, 1, 1], ...
'FaceColor', 'r',...
'EdgeColor', 'r')
end
break
elseif any(c2(i:i+9) > 0.9)
c2Idx = find(c2(i:i+9) > 0.9);
for j = 1:numel(c2Idx)
rectangle('Position', [c2Idx(j) - 0.5, 4.5, 1, 1], ...
'FaceColor', 'r',...
'EdgeColor', 'r')
end
break
end
Also change your A to
A = [c1(i:i+9); ep1(i:i+9); b(i:i+9); ep2(i:i+9); c2(i:i+9)];
For better readability.
.

Community Treasure Hunt

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

Start Hunting!

Translated by