Loop and plot in 2D
2 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a variable V(a,b,c) where
a=1:50
b=a
c=1:100
Now I would like to make a loop and plot the following:
sum_a(V(a,a+r,:))
May I ask you to help me with this loop and the 2D plot?
I have tried with this:
M=50;
V_prev=zeros(a,b,c);
V=V_prev;
for ik=1:M
V(ik,:,:)=V+V(ik,:,:);
for r=50-1:-1:0
V(ik,ik+r,:)=sum(V(ik,ik+r,:));
end
end
Unfortunately it doesn't work.
Thanks in advance for your reply.
4 件のコメント
Jan
2018 年 1 月 27 日
@Auryn_: I still do not get it. sum(M(:)) is the sum of all elements of the array M. Where does "l" come into play then?
If V is a V(a,a,b) array and you want to get the sum over all a, than you need:
sum(sum(V, 1), 2)
採用された回答
Image Analyst
2018 年 1 月 27 日
l is a terrible name for a variable. Anyway, you need to make sure a+l is not more than M
if (a+l) > M
break;
end
V(a,l,:)=sum(V(a,a+l,:));
0 件のコメント
その他の回答 (1 件)
Auryn_
2018 年 1 月 27 日
編集済み: Jan
2018 年 1 月 28 日
2 件のコメント
Jan
2018 年 1 月 28 日
I have edited your code. Please read https://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099.
Your code does not run: In the first iteration ik=1 and l=b-1 (did I told you already not to use "l" as name of a variable? It is a really serious advice.) ij is greater than a, such that A is not defined at all. Then the assignment to B fails with an error message.
参考
カテゴリ
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!