problem writing a seemingly easy summation

hi everyone I have this matrix P and for each column j I need to have a summation from second row to end, or i=2:end
for example
P=[1 3 5 7;3 5 7 9;2 4 6 8];
now the matrix I am looking for is [5 9 13 17]
but no matter how I try to write it the answer is second row only
can someone help?
thank u in advance

 採用された回答

Adam
Adam 2014 年 8 月 15 日

0 投票

sum( P(2:end,:) )

4 件のコメント

alexaa1989
alexaa1989 2014 年 8 月 15 日
it has to produce a (1,4) matrix not only a number in the example in need to produce [5 9 13 17] not sum of them
Adam
Adam 2014 年 8 月 15 日
It should produce your 1 by 4 matrix.
To be more explicit you can use:
sum( P(2:end,:), 1 )
but that is the same as the default behaviour, or at least it is when I run it.
alexaa1989
alexaa1989 2014 年 8 月 15 日
it just gives 37
Adam
Adam 2014 年 8 月 15 日
Can you paste in the whole Matlab commands with results output to command line? In the answer below you said it gave 34 so it sounds as though something odd is happening with your code or your Matlab.
I get exactly the same as Guillaume posted when I run this command.
sum (and most functions of that type) on a 2d matrix will, by default sum each column and give you a result which is 1-by-numColumns.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 8 月 15 日

0 投票

sum(P(2:end, :))

8 件のコメント

alexaa1989
alexaa1989 2014 年 8 月 15 日
it has to produce a (1,4) matrix not only a number in the example in need to produce [5 9 13 17] not sum of them
Guillaume
Guillaume 2014 年 8 月 15 日
have you tried the command? It produces exactly what you asked for
>>P=[1 3 5 7;3 5 7 9;2 4 6 8];
>>sum(P(2:end, :))
ans =
5 9 13 17
alexaa1989
alexaa1989 2014 年 8 月 15 日
yes I tried it produces 34 is my matlab not working correctly?
alexaa1989
alexaa1989 2014 年 8 月 15 日
it has this text when running
Warning: MATLAB Toolbox Path Cache is out of date and is not being used. Type 'help toolbox_path_cache' for more info
Adam
Adam 2014 年 8 月 15 日
rehash toolboxcache
should fix that I think
Guillaume
Guillaume 2014 年 8 月 15 日
Can you copy and paste EXACTLY what you wrote in matlab?
I doubt it has anything to do with the path cache (unless you've created your own sum function). Restart matlab to update the cache.
alexaa1989
alexaa1989 2014 年 8 月 15 日
is it possible that I have been using false data???!!!!
alexaa1989
alexaa1989 2014 年 8 月 15 日
how can i reset MATLAB for update?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

タグ

質問済み:

2014 年 8 月 15 日

コメント済み:

2014 年 8 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by