MATLAB Answers

0

How can i do the summation of every row?

Asked by Davide Conti on 5 Nov 2019
Latest activity Edited by Guillaume
on 6 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.

  5 Comments

Is the table actually a matlab table or some other container (matrix? cell array?). It's hard to know with your invented notation.
What is an independent row? Isn't column 111 the summation of all the other columns?
The table was imported from Excel and it's composed of cell arrays. I didn't invent any notation. It seemed useless to insert the whole table, i reported only the data to perform my work. Column 111 is the one I'm asking how to create. For each line I want to know how many are the numbers between columns 83 and 110 different from -1 (or >= 0).
Tables don't normally have numerical column names (it's only in the latest R2019b that you can have such names). This is the way a table is displayed in matlab:
>> demotable(1:4, [83:87, 110])
ans =
4×6 table
Var83 Var84 Var85 Var86 Var87 Var110
_____ _____ _____ _____ _____ ______
ID1 3 1 -1 -1 3 1
ID2 1 -1 3 4 1 4
ID3 0 3 0 0 1 1
ID4 -1 -1 2 0 2 4
which is a lot less ambiguous than what you have.
Anyway, I've already shown in my answer how to do what you want in just one line.
As I said, it doesn't look like you're using the features of a table, so perhaps you should be using a matrix, which in this case would be easier to work with.

Sign in to comment.

2 Answers

Answer by JESUS DAVID ARIZA ROYETH on 5 Nov 2019

replace yourtablename for the real name
value=sum(yourtablename{:,111})

  4 Comments

Show 1 older comment
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
now?
value=sum(yourtablename{:,111}>0)
Can you save the workspace and attach it here?
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

  0 Comments

Sign in to comment.