Creating a dynamical plot with a for loop

조회 수: 1 (최근 30일)
yoni verhaegen
yoni verhaegen 2015년 12월 3일
편집: yoni verhaegen 2015년 12월 3일
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