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 Kommentare

Walter Roberson
Walter Roberson am 27 Jan. 2018
In your duplicate question on this topic, Jan Simon commented:
I do not understand the question. What exactly is "V_a,V_(a+l)"?
Does "sum_a(V(a,a+l,:))" mean:
M = V(1:50, (1:50)+l, :)
sum(M(:))
? What is "l" here? If the 2nd dimension has a length of a, a+l exceeds the array dimensions.
By the way: Do not use "l" (lowercase L) as a name, because it is hard to distinguish from 1.
Auryn_
Auryn_ am 27 Jan. 2018
Hi,
yes, it means
M = V(1:50, (1:50)+l, :).
If I sum
sum(M(:)) does it sum in all "l"?
Because I don't want to sum over all "l" but over all a=1:50.
Thank you very much and sorry for any confusion.
Jan
Jan am 27 Jan. 2018
@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)
Auryn_
Auryn_ am 27 Jan. 2018
Hi, I have corrected the main text. Hopefully is more clear now! Thanks.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 27 Jan. 2018

0 Stimmen

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,:));

Weitere Antworten (1)

Auryn_
Auryn_ am 27 Jan. 2018
Bearbeitet: Jan am 28 Jan. 2018

0 Stimmen

Now it works
for ik=1:a
for l=100-1:-1:0
ij=ik+l
if ij > a
break;
end
A(ik,ij,:)=V(ik,ij,:);
end
B(ik,:,:)=sum(A(ik,:,:));
end

2 Kommentare

Jan
Jan am 28 Jan. 2018
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.
Auryn_
Auryn_ am 28 Jan. 2018
If we consider
a=1:100
and
a=b,
it works.
Thanks again for everything!

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 26 Jan. 2018

Kommentiert:

am 28 Jan. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by