How to store values from nested for loop

44 views (last 30 days)
Im trying to make a nested loop but the value of A keeps adding up after each loop. I think i should store the value of A after each loop so it doesn't add up but im not sure how i do that.
clear all
N=10;
A=zeros(1,N);
for m = 1:N;
for n = 1:N
A(n,m) = A(n)+ sin(pi*(m+n)/(2*N))*sin((pi*m)/N);
end
end
A
  4 Comments
DGM
DGM on 22 Sep 2021
This suggests that A is a 1xN vector:
A = zeros(1,N);
This says that A is a NxN matrix instead:
for m = 1:N;
for n = 1:N
A(n,m) = % ...
end
end
... but this
A(n)+ sin(pi*(m+n)/(2*N))*sin((pi*m)/N);
and your comment suggest that A is either a vector or is being addressed with linear indexing -- but if you're using linear indexing to address the matrix, why wouldn't it keep adding up?
I doubt you intend to use linear indexing, and I'm not sure if you're intending for the output to be a matrix either. The example you give is a simple 1-D scenario where the output of a particular iteration is a function of the prior result. The code above doesn't do that. It uses A(n,1) implicitly as Jan pointed out, because n in A(n) is treated as a linear index.
There are two points of uncertainty that I see:
  • Is A supposed to be 1D or 2D?
  • If 2D, to which element of A is "A(n)" intended to refer?
It's hard to guess what's intended. If the result isn't supposed to keep recycling values after each inner loop, then the behavior is probably supposed to only be working along one axis. For example, let's say you intend for a 2D output, where each element is a function of the element one row above. In other words, "A(n)" in the above expression for A(n,m) actually refers to A(n-1,m). Working on those assumptions:
N = 10;
A = zeros(N,N);
A(1,:) = sin(pi*(2:N+1)/(2*N)).*sin((pi*(1:N))/N);
for m = 1:N
for n = 2:N
A(n,m) = A(n-1,m) + sin(pi*(m+n)/(2*N))*sin((pi*m)/N);
end
end
A
% that whole thing simplifies to this
m = 1:N;
A2 = zeros(N,N);
A2(1,:) = sin(pi*(m+1)/(2*N)).*sin((pi*m)/N);
for n = 2:N
A2(n,:) = A2(n-1,:) + sin(pi*(m+n)/(2*N)).*sin((pi*m)/N);
end
immse(A-A2) % outputs are identical
Or maybe it's supposed to do something else. If the above assumption isn't close, we have to fall back to answering those two bulleted questions.
Berghan
Berghan on 28 Sep 2021
Edited: Rik on 28 Sep 2021
I asked a guy for help and he said that what i have done is almoste correct. For each iteration in the loop i get the value it had before + something, but then he says that it should be a number and not a vector. He says it's okay for it to be a vector aswell but then i have to sum it up afterwards.
here is a picture of the equation we are using.
Im really trying to understand how I fix this code but I keep getting diffrent responds to whats wrong with the code. This is all new to me (both the double sum math and the coding) so it keeps getting messed up in my brain.
here is an uppdate on the code from the start of post if it helpt to clarify what I am trying to achieve.
Clear all
N = 10;
A = zeros(1,N);
for m = 1:N
for n = 1:N
A(n) = A(n) + sin(pi*(m+n)/(2*N))*sin((pi*m)/N);
end
B=A(n);
end
C=(B)/(N^2);
format long
display(C);

Sign in to comment.

Accepted Answer

Rik
Rik on 28 Sep 2021
Edited: Rik on 28 Sep 2021
The first step to implement a summation in Matlab is very easy: just use a for loop. It is often possible to do this with a matrix operation, but let's first do the first step.
%define constants here
N=10;
%Initialize the sum value to 0.
S=0;
%The sum-operator is very similar to a for statement:
%it defines a variable with a starting value and an end point.
for m=1:N
for n=1:N
%Now all variable are defined, you can simply write the inner part
%in Matlab syntax:
val=sin(pi*(m+n)/(2*N))*sin(pi*m/N);
S=S+val;
end
end
%Now do the product in front:
S=S*1/N^2;
format long,disp(S)
0.535931976159747
You can do this in one go, but you'll have to make sure the functions you're using inside the summation actually all support array inputs. Luckily for you, they do in this case (with minor modifications).
clearvars
N=10;
%define m and n as arrays:
[m,n]=ndgrid(1:N,1:N);
val=sin(pi*(m+n)/(2*N)).*sin(pi*m/N);
% ^
% This is the only place where two arrays interact.
% If in doubt, replace all / with ./ (same for * and ^)
% those will do the operation element by element, instead of a matrix
% multiplication.
S=(1/N^2)*sum(val,'all'); %or sum(val(:)) on old releases
disp(S)
0.535931976159747

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!