Asked by Davide Conti
on 5 Nov 2019

Dear Experiences

i have a table that look like the following:

Table (from column 83 to 110):

Name ... 83 84 85 86 87 ..... 110

ID1 1 -1 0 -1 0 -1

ID2 4 2 3 -1 0 -1

ID3 0 -1 2 -1 3 -1

ID4 2 2 2 -1 0 -1

what I want to do is to know how many are the numbers >= 0, in column 111

Name ... 83 84 85 86 87 ..... 110 111

ID1 1 -1 0 -1 0 -1 3

ID2 4 2 3 -1 0 -1 4

ID3 0 -1 2 -1 3 -1 3

ID4 2 2 2 -1 0 -1 4

where the column 111 involve the summation of every independent row,

thanks for any help.

Answer by JESUS DAVID ARIZA ROYETH
on 5 Nov 2019

replace yourtablename for the real name

value=sum(yourtablename{:,111})

Davide Conti
on 5 Nov 2019

DATA is the mytablename.

Why is this cycle wrong?

for i = 2:size(DATA,1)

for j = 83:110

DATA{i,111} = sum((DATA{i,j} >= 0));

end

end

JESUS DAVID ARIZA ROYETH
on 5 Nov 2019

now?

value=sum(yourtablename{:,111}>0)

Can you save the workspace and attach it here?

Guillaume
on 5 Nov 2019

sorry but it doesn't work and I can't understand why

we'd stand a chance of understanding why if you told us what's happening. If you get an error message then give us the full text of the error message. If you don't get the expected result, what did you get and how did it differ from what you expected.

Why is this cycle wrong?

Because you're overwriting Data{i, 111} at each j. And you're passing a scalar to sum. The sum of a scalar is that scalar.

Sign in to comment.

Answer by Guillaume
on 5 Nov 2019

Edited by Guillaume
on 6 Nov 2019

Guessing at what you're trying to do from your loop code:

Data{:, 111} = sum(Data{:, 83:110} >= 0, 2);

It doesn't look like you're using any feature of a table, so if everything is numeric in your table, you'd be better off storing the data in a matrix. The equivalent matrix syntax of the above is:

yourmatrix(:, 111) = sum(yourmatrix(:, 83:110) >= 0, 2);

slightly edited the answer now that we have clarification of what's needed

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## KALYAN ACHARJYA (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/489330-how-can-i-do-the-summation-of-every-row#comment_763917

## Davide Conti (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/489330-how-can-i-do-the-summation-of-every-row#comment_763928

## Guillaume (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/489330-how-can-i-do-the-summation-of-every-row#comment_763935

## Davide Conti (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/489330-how-can-i-do-the-summation-of-every-row#comment_764058

## Guillaume (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/489330-how-can-i-do-the-summation-of-every-row#comment_764194

Sign in to comment.