Multiple Sums Through a For Loop

Hi,
I have the following code:
deg = 2;
poly = 11;
numpt = deg+1;
upt = (poly*deg)+1;
a = -2;
b = 2;
A = zeros(numpt,poly);
x = linspace(a,b,upt);
A(:,1) = x(1:numpt)';
A(:,2) = x(3:5);
for j = 2:poly
A(:,j) = x(j * deg - numpt + 2 : j * deg +1)';
end
v = A;
B = 1 ./ (1+ 25*v.^2);
N = length(v);
M = 255;
N1 = numpt;
C = zeros(N,M);
W = zeros(M,length(v));
a1 = min(v);
b1 = max(v);
for j = 1:length(v)
W(:,j) = linspace(a1(:,j),b1(:,j),M);
end
W = W';
%%%%%%%%%Below this line is what is being modified %%%%%%%%%
for i = 1:N
for j = 1:N1
prod2 = 1.0;
for k = 1: N1
if k ~= j
prod2 = prod2.*(W(i,:) - v(k,i))./(v(j,i) - v(k,i));
end
end
C(i,:) = prod2;
D(:,j) = B(j,11)*C(11,:) % specific to column 11 in matrix B
end
end
DD = sum(D,2);
Goal: somehow generate a 255 x 3 matrix, 11 times, and run a summation of the rows in each of the 3 columns, N1 times i.e. 11 times instead of each time manually.
I'm trying to run these two lines in the above code at the same time in a for loop:
D(:,j) = B(j,11)*C(11,:) % specific to column 11 in matrix B
DD = sum(D,2);
Meaning multiply values of matrix C (size = 11x255) and matrix B (size = 3x11) (do this N1 times i.e. 11 times since there are 11 columns in matrix B) and then store this into matrix D (size = 255 x 3) and then sum each of the 255 row values of matrix D so matrix D becomes (size = 255 x 1)
I'm trying to consolidate all of this code into one main for loop, or nested / multiple loops if needed.
I used column 11 in matrix B above, just as a sample test to see if the code worked manually.
I want to apply the operation to all of the columns in matrix B i.e. 1 to 11 or 1:N1.
%%%%%%%%%%%Tried something like this but it's not working
for i = 1:N
for p = 1:N1
E(:,i) = sum(B(p,i)*C(i,:),1);
end
end
%%%%%%%%%%%Tried something like this but it's not working
Any advice?
Thanks.

3 Kommentare

dpb
dpb am 24 Jul. 2017
I spent some time the other day trying to decipher what the objective really is to see if could do something but struck out...
Can you illustrate just what the inputs are and the expected output for a SMALL sample size so can see what it is that is actually the desired output? What if poly were only 3 or 4, would the same logic hold and the output then be small enough to actually study?
As is, I can't decipher what parts are what you want and which aren't nor what the desired output would be...
John Stulich
John Stulich am 26 Jul. 2017
Bearbeitet: John Stulich am 26 Jul. 2017
Hi,
Yes. Ultimately the code should be able to work for smaller number of polynomials i.e. 3 or 4.
The end result is an output of a normally distributed graph of a curve that is created through a piece-wise distribution of smaller curves.
Here is the code I created to "manually" obtain the output for a smaller number of polynomials i.e. 4 polynomials (the resulting curve becomes smoother as the number of polynomials increases i.e. the curve is smoother for 11 polynomials):
deg = 2;
poly = 4;
numpt = deg+1;
upt = (poly*deg)+1;
a = -2;
b = 2;
A = zeros(numpt,poly);
x = linspace(a,b,upt);
A(:,1) = x(1:numpt)';
A(:,2) = x(3:5);
for j = 2:poly
A(:,j) = x(j * deg - numpt + 2 : j * deg +1)';
end
v = A;
B = 1 ./ (1+ 25*v.^2);
N = length(v);
M = 255;
a1 = min(v);
a2 = max(v);
% Min values of individual row vectors
min1 = a1(1,1); % 1
min2 = a1(1,2); % 3
min3 = a1(1,3); % 5
min4 = a1(1,4); % 7
% Max values of individual row vectors
max1 = a2(1,1);
max2 = a2(1,2);
max3 = a2(1,3);
max4 = a2(1,4);
% Original matrix values split into column vectors
v1 = v(:,1); % Column vector of column 1 of v
v2 = v(:,2); % Column vector of column 2 of v
v3 = v(:,3);
v4 = v(:,4);
% Function matrix values split into column vectors
b1 = B(:,1); % Column vector of column 1 of D
b2 = B(:,2); % Column vector of column 2 of D
b3 = B(:,3);
b4 = B(:,4);
% 1x255 row vectors
w1 = linspace(min1, max1, M);
w2 = linspace(min2, max2, M);
w3 = linspace(min3, max3, M);
w4 = linspace(min4, max4, M);
N1 = length(v1);
C = zeros(M,N1); % 255 x 3
for j = 1:N1
prod = 1.0;
for k = 1: N1
if k ~= j
prod = prod.*(w1 - v1(k))./(v1(j) - v1(k));
end
end
C1(:,j) = b1(j)*prod;
end
D1 = sum(C1,2);
for j = 1:N1
prod = 1.0;
for k = 1: N1
if k ~= j
prod = prod.*(w2 - v2(k))./(v2(j) - v2(k));
end
end
C2(:,j) = b2(j)*prod;
end
D2 = sum(C2,2);
for j = 1:N1
prod = 1.0;
for k = 1: N1
if k ~= j
prod = prod.*(w3 - v3(k))./(v3(j) - v3(k));
end
end
C3(:,j) = b3(j)*prod;
end
D3 = sum(C3,2);
for j = 1:N1
prod = 1.0;
for k = 1: N1
if k ~= j
prod = prod.*(w4 - v4(k))./(v4(j) - v4(k));
end
end
C4(:,j) = b4(j)*prod;
end
D4 = sum(C4,2);
plot(v1, b1, 'or', w1, D1,'-b',...
v2, b2, 'or', w2, D2,'-b',...
v3, b3, 'or', w3, D3,'-b',...
v4, b4, 'or', w4, D4,'-b',...
'Linewidth', 1.5, 'MarkerSize', 8)
So my goal was to condense all of those multiple for loops into one main loop with nested loops, so the user could simply input the desired degree of polynomials and input the desired number of polynomials, while the code did the rest. The user could also input the x value (i.e. by changing "a" and "b") which is currently based set to linspace values.
I was able to condense most of these for loops into one main for loop and condense the code (my originally posted code), except for the summation lines which is where I was stuck at.
dpb
dpb am 26 Jul. 2017
Bearbeitet: dpb am 27 Jul. 2017
How about 'splaining the logic w/ some comments and/or state the problem definition/algorithm in words rather than having to try to decipher the code only with no comments?(*) If had an actual problem statement then could attack it from first principles instead of just trying to optimize a given implementation. That's what I was hoping would come from previous comment/question...
(*) The comments are suggestive of what the specific operation or line of code does which is mostly self-evident; what they don't explain is the underlying alogorithm...I know it's some sort of attempt at approximating a normal, but the remainder relies on trying to decipher the code...is this from some published reference or some other description?
I can generate A in a few lines; it's not clear precisely where to go next yet having such limited time to pore over the code...
>> i1=[1:deg:length(x)-deg].';
>> i2=i1+deg;
>> A=cell2mat(arrayfun(@(i1,i2) x(i1:i2),i1,i2,'uniform',0)).'
A =
-2.0000 -1.0000 0 1.0000
-1.5000 -0.5000 0.5000 1.5000
-1.0000 0 1.0000 2.0000
>>
I'll try to find some more time this evening...

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Gefragt:

am 23 Jul. 2017

Bearbeitet:

dpb
am 27 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by