Creating a dynamical plot with a for loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Imagine we quickly create a matrix with some values in it, called 'number':
matrix1=rand(10);
m=0.5;
number=zeros(size(matrix1));
for i=2:9
for j=2:9
number1(i,j)=(matrix1(i,j+1).*m);
number2(i,j)=(matrix1(i+1,j).*m);
number(i,j)=sqrt((number1(i,j))^2+(number2(i,j))^2);
end
end
imagesc(number)
You can see that the value of number(i,j) is dependent on m. Now i want to make a plot where m values vary in function of the day of the year.
mnew=zeros(365,1);
for s=1:365
mnew(s)=m*s;
end
I now got number(i,j) with the values that should be dependent on m, and a file with the temporal evolution of m throughout the year. How can i now create a dynamic graph that will show the evolution of number(i,j) throughtout the year (as a function of varying m)?
Thanks in advance!
2 commentaires
Image Analyst
le 3 Déc 2015
So you have 8 i and 8 j - so that's 64 values for "numbers", but 365 m values. So which 64 m values do you want to use out of the 365 available to you?
Réponse acceptée
jgg
le 3 Déc 2015
I think you could try just make a function, calling number_func(m) which nests the code above.
Then, evaluate number_func for your values of m, storing the output matrix.
Then, you can just plot for each (i,j) the values versus m in any plot you want. That seems like the simplest way.
However, you can get crazy in the following way by animating it. I think this looks really neat:
matrix1=rand(10);
m=0.5;
number=zeros(size(matrix1));
for k = 1:365
m_s = m*k
for i=2:9
for j=2:9
number1(i,j)=(matrix1(i,j+1).*m_s);
number2(i,j)=(matrix1(i+1,j).*m_s);
number(i,j)=sqrt((number1(i,j))^2+(number2(i,j))^2);
numbers{k} = number;
end
end
end
for k = 1:365
image(numbers{k});
M(k) = getframe;
end
figure
movie(M,5)
if you actually use this code you'll want to pre-allocate the memory but just as a demo this seems good.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!