Averaging rows with same name

9 ビュー (過去 30 日間)
Ross Ross
Ross Ross 2018 年 3 月 4 日
コメント済み: peregrin12 2018 年 10 月 5 日
Hi everyone, I m new with Matlab, I hope you can help me with that. I have a matrix with more than 20 000 spectra (rows) containing spectra collected for different experiments. I have the name of experiments in the first column. I would like to create a loop which will allow to average the experiments based in their names. Basically average all rows with same name, taking into account that the number of spectra per experiment are not the same. So, basically I would like to obtain at the end a new matrix with mean spectra per experiment.
I am using the following:
X= data matrix containing name and spectra
[a b c] =unique(X(:,1));
Y=splitapply(@mean,X(:,2:end),c)
I obatin the result I expected, I think, but my question now is, that is better than a loop ? and if yes, why ? thanks
  1 件のコメント
peregrin12
peregrin12 2018 年 10 月 5 日
When I tried this code, I got the following error message:
Error using splitapply
Applying the function 'mean' to the 1st group of data generated the following error:
Invalid data type. First argument must be numeric or logical.
Error in Average_spectracode Y=splitapply( @mean, X, c);
I'm also using a data matrix of spectra with their names as the first column.
What am I doing wrong?

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

回答 (2 件)

Stephen23
Stephen23 2018 年 3 月 4 日
編集済み: Stephen23 2018 年 3 月 4 日
" my question now is, that is better than a loop ? and if yes, why ?"
Loops (with preallocated arrays) are a perfectly acceptable way to perform tasks, and there is no reasons not to use them whenever you want to. However I would suggest that in this case using splitappyl is a slightly better solution:
  • it is more compact, which means that overall the code could be written to more clearly represent the flow of the program.
  • less code means fewer mistakes, it is easier to comprehend a shorter command and understand that it does what you want.
  • you get to practice using tools that MATLAB has available: if you stick to using loops for solving everything then in ten years you will still be writing code using loops and using MATLAB as if it was a big calculator. Learn how to use MATLAB features to your advantage: the more you practice using them, the easier they are to use, the better you will understand how to use them.
  • and based on this you can speed up your code development, by using high-level commands which are more compact. These will get more intuitive as you use them more.
  • it is easier to write generalized code: using a high-level command often makes it clear that swapping a hard-coded index or fieldname for a function input will let you place common code into subfunctions.
  1 件のコメント
Ross Ross
Ross Ross 2018 年 3 月 4 日
thank you very much Stephen for the explanation, it was really useful.

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


David Fletcher
David Fletcher 2018 年 3 月 4 日
Generally vectorized code will run faster than equivalent solutions with loops. Though, personally I don't find it all that intuitive to follow and debug (though deeply nested loops can also be horrendous to unravel), so unless I have a need for something to run as quickly as possible or I'm working with large data sets, I still tend to use loops.
  1 件のコメント
Ross Ross
Ross Ross 2018 年 3 月 4 日
Thank you for your answer David

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by