How can i do the summation of every row?

1 次查看(过去 30 天)
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 个评论
Davide Conti
Davide Conti 2019-11-6
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).
Guillaume
Guillaume 2019-11-6
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.

请先登录,再进行评论。

回答(2 个)

JESUS DAVID ARIZA ROYETH
replace yourtablename for the real name
value=sum(yourtablename{:,111})
  4 个评论
JESUS DAVID ARIZA ROYETH
now?
value=sum(yourtablename{:,111}>0)
Can you save the workspace and attach it here?
Guillaume
Guillaume 2019-11-5
编辑:Guillaume 2019-11-5
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.

请先登录,再进行评论。


Guillaume
Guillaume 2019-11-5
编辑:Guillaume 2019-11-6
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by