Problem with symsum function: doesn't work with matrix elements?

I am trying to sum elements of a matrix with the symsum function.
E = squareform(pdist(ConcernedNodes2(:,1:2)));
DistMin = min(DistRoadMetro,[],3);
N = length(ConcernedNodes2);
V = zeros(length(ConcernedNodes2),timeSteps);
for t = 1:timeSteps
C = perc_road_num(:,:,t);
for i=1:length(ConcernedNodes2)
V(i,t) = (1/(symsum(E(i,k),k,1,length(ConcernedNodes2))*(N-1)))*symsum(E(i,j).*C(i,j)/(DistMin(i,j)),j,1,length(ConcernedNodes2));
end
end
And I get this error message: Undefined function 'symsum' for input arguments of type 'double'.
Can someone help me please?

6 comentarios

Perhaps you want to use cumsum().
Nope, I need the sum of all elements of line i of the matrices in the equation.
Then why not just use sum() since it's one dimensional inside the loop?
Habib
Habib el 7 de Mzo. de 2019
Editada: Habib el 7 de Mzo. de 2019
for t = 1:timeSteps
C = perc_road_num(:,:,t);
for i=1:length(ConcernedNodes2)
V(i,t) = (1./sum(E(i,1:length(ConcernedNodes2))*(N-1))).*sum(E(i,1:length(ConcernedNodes2)).*C(i,1:length(ConcernedNodes2))./(DistMin(i,1:length(ConcernedNodes2))));
end
end
I tried this, but still doesn't work.
It runs but the results are NaN
I suspect some of those sum come out as 0
Habib
Habib el 7 de Mzo. de 2019
Editada: Habib el 7 de Mzo. de 2019
for t = 1:timeSteps
C = perc_road_num(:,:,t);
for i=1:length(ConcernedNodes2)
V2(i,t) = (1./sum(E(i,1:length(ConcernedNodes2)~=i)*(N-1))).*sum(E(i,1:length(ConcernedNodes2)~=i).*C(i,1:length(ConcernedNodes2)~=i)./(DistMin(i,1:length(ConcernedNodes2)~=i)));
end
end
This works, I forgot to add ~=i in the sum

Iniciar sesión para comentar.

Respuestas (1)

Adam Danz
Adam Danz el 7 de Mzo. de 2019
Editada: Adam Danz el 7 de Mzo. de 2019
The first two inputs to symsum() should be symbolic. It's difficult to interpret what the code is supposed to do without knowing what's stored in the variables.
More info in symsum inputs:
Examples:
It seems like you don't need symsum() in the first place. In this segment below, what I think you're trying to do is sum part of a row of matrix 'E'. It looks like you're trying to take the sum of row 'i' from column 1 to column length(ConcernedNodes2).
symsum(E(i,k),k,1,length(ConcernedNodes2)) % <--- incorrect
If that's the case, what you really want is
sum(E(i,1:length(ConcernedNodes2))) % <--- correct
which will give you one value (the sum of those numbers). Or, perhaps you are looking for cumsum() which would be cumulative sum and would output a vector the same length as the segment you're isolating.

4 comentarios

Sorry, E is a matrix
So which function should I use?
I've updated my answer with suggestions.
Habib
Habib el 7 de Mzo. de 2019
Editada: Habib el 7 de Mzo. de 2019
Actually this worked well:
for t = 1:timeSteps
C = perc_road_num(:,:,t);
for i=1:length(ConcernedNodes2)
U1=0;
U2=0;
for j = 1:length(ConcernedNodes2)
if j ~=i
U1 = U1 + E(i,j)*C(i,j)/(DistMin(i,j));
U2 = U2 + E(i,j);
end
end
V(i,t) = 1/(U2*(N-1))*U1;
end
end
And it is quicker than using the sum function :)

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 7 de Mzo. de 2019

Editada:

el 7 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by