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
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?
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!