Creating a dynamical plot with a for loop

1 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2015-12-3
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?
yoni verhaegen
yoni verhaegen 2015-12-3
编辑:yoni verhaegen 2015-12-3
number(i,j) is the matrix where the spatial distribution of the values created in the first loop is shown (64 values). However, these are dependent on m. I want to create a dynamic graph which shows how the values of number(i,j) will change when m changes, thus showing the temporal evolution (365 values of m since there are 365 days in a year). So m is dependent on the day of the year (second loop) and I want to implement this in the graph.
Iteration 1 -> m = 0.5 -> number(1,1) = ... number (1,2) = ..., number(1,3) = ... etc.
Iteration 2 -> m = 1 -> number(1,1) = ... number (1,2) = ..., etc.
Iteration 3 -> m = 1.5 -> number(1,1) = ... number (1,2) = ..., etc.

请先登录,再进行评论。

采纳的回答

jgg
jgg 2015-12-3
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by