Multiple Sums Through a For Loop
Ältere Kommentare anzeigen
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
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
am 26 Jul. 2017
Bearbeitet: John Stulich
am 26 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...
Antworten (0)
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!