Averaging rows with same name
조회 수: 9 (최근 30일)
이전 댓글 표시
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
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
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.
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!