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 件のコメント
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
2019 年 11 月 5 日
replace yourtablename for the real name
value=sum(yourtablename{:,111})
4 件のコメント
JESUS DAVID ARIZA ROYETH
2019 年 11 月 5 日
now?
value=sum(yourtablename{:,111}>0)
Can you save the workspace and attach it here?
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!