How to store values from nested for loop
46 次查看(过去 30 天)
显示 更早的评论
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 个评论
DGM
2021-9-22
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.
采纳的回答
Rik
2021-9-28
编辑:Rik
2021-9-28
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)
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 个)
另请参阅
类别
在 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!