Fast way of performing function on multiple columns of data?
이전 댓글 표시
Hi,
I have multiple columns of data on which I need to perform the same function in Matlab. The function is exactly the same, all that changes is the data on which the function is performed on.
I have around 500 columns. Is there any shortcut which will automatically give me the value from the function instead of me having to select each column and performing the function separately.
Thank you!
댓글 수: 2
James Tursa
2015년 3월 18일
Is the output of the function a column the same size as the input column?
Adam
2015년 3월 18일
Have you benchmarked how long it takes to just run a for loop over all the columns? There are plenty of flashy looking ways people attempt just because they have heard for loops are bad in Matlab, but quite a few of them are slower than raw for loops even though they look fancier. You should at least know how long the simple for loop approach takes in order to compare alternative methods.
답변 (3개)
A simple way is to split your matrix in a cell array of columns using num2cell and use cellfun on this cell array:
M = rand(500);
cellfun(@somefunction, num2cell(M, 1)) %apply myfun to every column of M
The exact syntax of cellfun depends on what arguments you have to pass to your function and what return values you get.
댓글 수: 1
Thanks for the responses :) My function is essentially this link below. http://www.mathworks.com/matlabcentral/fileexchange/26546-approximate-entropy.Do you think your advice can be applied to this? Im beginning to fear ill have to spend hours doing each one individually.
Of course, you don't need to:
M = rand(500); %for example
window_length = 5; %for example
similarity_measure = 0.5; %for example
entropy_by_column = cellfun(@(col) approx_entropy(window_length, similarity_measure, col), num2cell(M, 1))
Image Analyst
2015년 3월 18일
Just extract a column and call that function. Put into a loop to process all your columns.
[rows, columns] = size(yourData);
for col = 1 : columns
thisColumn = yourData(:, col);
columnEntropy(col) = approx_entropy(5, 0.5, thisColumn);
end
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!