MATLAB Answers

How to sum my matrix element?

3 views (last 30 days)
I have problem with my matrix. I cannot sum my matrix element 'kl', this is my code
clc;
clear all;
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0];
y=[190 187 188 189 190 190 190 190 190 190 192 193 193 193 193 196 196 197 198 200 196 197 197 198 196 192 194 194 193 193 195 196 196 199 197 196 195 196 195 196 196 196 195 194 193 188 190 190 193 190 188 186 185 184 182 185 184 183 183 181 184 185 185 184 185 185 188 189 190 189 187 187 188 188 189 189 188 186 183 176 174 181 181 177 175 175 173 176 178 177 174 174 174 173 173 174 174 178 178 180];
%x=[1 2 3 4 5 6 7 8 9 10 11 12];
%y=[99 87 99 73 121 69 141 141 92 158 93 145];
n =100;
my=sum(y);
m = sum(bsxfun(@power,x(:),1:(2*n)),1);
myy = sum(bsxfun(@power,y(:),1:(2*n)),1);
xv = x(1:n);
yv = y(1:n);
ny = sum(bsxfun(@times,bsxfun(@power,xv(:),1:99),yv(:)),1);
for na=2:83
for i=1:na
for j=1:na
for k=1:na
A(1,1)=n;
if i>1
A(i,1)=m(i-1);
end
if k>1
A(i,k)=m(k-1);
if A(i,k)==A(i,k-1)
A(i,k)=m(k);
elseif A(i,k)<A(i,k-1)
A(i,k)=m(i+k-2);
end
end
end
end
end
B=[my 1];
for i=2:na
b = ny(i-1);
B(i)=b;
end
for k=1:na-1
for i=k+1:na
if A(i,k)~=0
lambda=A(i,k)/A(k,k);
for j=1:na
A(i,j)=A(i,j)-lambda*A(k,j);
end
B(i)=B(i)-lambda*B(k);
end
end
end
for i=na:-1:1
sum=0;
X(i)=0;
for j=1:na
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
for c = 1:100
R(c) = 0;
for d = 1:na
R(c) = R(c)+X(d)*x(c)^(d-1);
end
end
na;
R;
end
for i=1:83
kl(i)=X(i)*0.1^(i-1);
end
kl
sum(kl)

  0 Comments

Sign in to comment.

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 24 Mar 2020
Hi Eddy,
It is because in one of the for loop, you made sum as a variable too. Using the same variable which is the same as inbuilt function is not encouraged and lead to inappropriate conditions as such.
So, update this loop here and this should solve:
for i=na:-1:1
sumOut=0; % Replace sum with other variable
X(i)=0;
for j=1:na
sumOut=sumOut+A(i,j)*X(j);
end
X(i)=(B(i)-sumOut)/A(i,i);
end
Hope this helps.
Regards,
Sriram

  1 Comment

Eddy Iswardi
Eddy Iswardi on 24 Mar 2020
thank you, it's work.

Sign in to comment.

More Answers (1)

Sindar
Sindar on 24 Mar 2020
from sum documentation:
"S = sum(A,'all') computes the sum of all elements of A. This syntax is valid for MATLAB® versions R2018b and later."

  3 Comments

Eddy Iswardi
Eddy Iswardi on 24 Mar 2020
sorry, It's not working for my matlab version 2016a
Sindar
Sindar on 24 Mar 2020
Use:
sum(kl(:))
or
sum(sum(kl))
for older Matlab (not sure which is faster)
But, also make sure you don't use "sum" as a variable name, as explained by Sriram
Eddy Iswardi
Eddy Iswardi on 24 Mar 2020
thank you. it's work too. now, I have some codes with same function.

Sign in to comment.

Sign in to answer this question.

Tags