If function with cellfun (i.e. vectorized code instead of ridiculously slow loop).

조회 수: 2 (최근 30일)
Pierre Lonfat
Pierre Lonfat 2018년 3월 14일
댓글: Guillaume 2018년 3월 14일
Hello everyone. I am new to matlab and really try to optimize my code since it takes to much time to run. I don't really understand the concept behind cellfun yet. If we take the following example:
I would have no idea how to write it in vectorized code since I have to go through every cells and then through every lines of each cell.
So is it possible to write it in vectorized code and if yes can anyone help me with that ?
  댓글 수: 6
Adam
Adam 2018년 3월 14일
cellfun is rarely, if ever, faster than a loop, it is just a different way of doing it which may be considered neater or may not. Moving away from using cells in the first place is usually a big step towards speeding up code if you can possibly use numeric arrays instead.
Guillaume
Guillaume 2018년 3월 14일
You're using 2D indexing {row, col} with one of the dimension looking like it's always 1 as opposed to linear indexing {idx}. Is your data actually 2D or 1D?
The difference in speed between the two is negligible but I personally find it confusing when people use 2D indexing with 1D data. Plus, this add a hidden assumption that the cell array is a row vector and the content of each cell is a column vector, whereas with linear indexing the code works regardless of the direction of the vector.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by