Sum of array items into another array

I need to find the sum of items within each of the 360 arrays and then make another array with just the sums.

8 件のコメント

James Tursa
James Tursa 2016 年 6 月 22 日
You haven't given us enough information to help you. What specific items? How are your arrays stored? You need to give us a short example that shows exactly what you want to do.
Kirby Fears
Kirby Fears 2016 年 6 月 22 日
Are your 360 arrays floating around separately in your workspace, or are they all part of one cell array, struct, or ND+1 array?
np
np 2016 年 6 月 22 日
they are all separately in the workspace.
ex. data.speed1 data.speed2 . . . data.speed360
they are all 1x30
i want to find the sum of all the thirty items in each and create an other array thats 1x360 that i can plot
Kirby Fears
Kirby Fears 2016 年 6 月 22 日
編集済み: Kirby Fears 2016 年 6 月 22 日
That looks like elements of a struct or columns of a table. What is the type associated with "data" in your workspace?
np
np 2016 年 6 月 22 日
its just numbers like ranging from 0 to 0.0170
mostly decimals
Kirby Fears
Kirby Fears 2016 年 6 月 22 日
編集済み: Kirby Fears 2016 年 6 月 22 日
np,
You indicated the data is in your workspace as data.speed1, data.speed2, etc.
In your workspace window, what is the data type of "data"? If it has fields speed1, speed2, etc, then "data" must be a table or struct. Please let me know which type it is so I can help.
np
np 2016 年 6 月 22 日
the speed1 speed2 are just the names of the arrays that are there
the values inside of them are numeric
Kirby Fears
Kirby Fears 2016 年 6 月 22 日
I understand that the values inside of speed1 and speed2 are numeric, but you wrote your example as "data.speed1". So are speed1 and speed2 contained in a workspace variable called data or not?

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

 採用された回答

Kirby Fears
Kirby Fears 2016 年 6 月 22 日

3 投票

Assuming data is a struct with fields speed1, speed2, etc.
sumArray = structfun(@sum,data);

その他の回答 (2 件)

Guillaume
Guillaume 2016 年 6 月 23 日

1 投票

If your structure has fields other than the speed fields, you can loop over a hardcoded number of speed fields:
numspeed = 360;
speedsum = zeros(numspeed, 1);
for speedidx = 1:numspeed
speedsum(speedidx) = sum(data.(sprintf('speed%d', speedidx)));
end
Shameer Parmar
Shameer Parmar 2016 年 6 月 23 日

0 投票

for i=1:length(fields(data))
s(i) = sum(eval(['data.speed',num2str(i)]));
end

6 件のコメント

Guillaume
Guillaume 2016 年 6 月 23 日
編集済み: Guillaume 2016 年 6 月 23 日
DO NOT USE EVAL. Please don't provide solutions that use eval, that's just encouraging bad programming practices, particularly when there are better solution.
In this particular case, using dynamic field names does the same without any need for eval:
s(i) = sum(data.(['speed',num2str(i)]));
Also, the proper method for obtaining the fields of a structure is fieldnames. fields is a undocumented function that may disappear or change behaviour in future versions of matlab.
In any case, Kirby's answer is much better.
Stephen23
Stephen23 2016 年 6 月 23 日
編集済み: Stephen23 2016 年 6 月 23 日
Avoid using eval for trivial code like this. Here is why:
It would be much better to use the functionality that MATLAB already has to allow fieldnames to be defined via strings:
np
np 2016 年 6 月 23 日
it says: Reference to non-existent field 'speed361'.
how do i just make it stop at speed360
np
np 2016 年 6 月 23 日
i used:
for i=1:length(fields(data))
s(i) = sum(data.(['speed',num2str(i)]));
end
Guillaume
Guillaume 2016 年 6 月 23 日
1) Use fieldnames instead of fields. That won't solve the immediate problem as they both provide the same result, but fields is not supported whereas fieldnames is.
2) You get this error because your data has fields other than the 360 speed fields.
Shameer Parmar
Shameer Parmar 2016 年 6 月 24 日
Hello np..
I guess your length of fields(data) is more that 360, that why..
If yes, you can apply one more filter like..
for i=1:length(fields(data))
if i<=360
s(i) = sum(data.(['speed',num2str(i)]));
end
end

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

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

タグ

質問済み:

np
2016 年 6 月 22 日

コメント済み:

2016 年 6 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by