How to vectorize this code?
显示 更早的评论
I try to use bsxfun, but maybe it does not suit this purpose.
C = bsxfun(@function,A(AIterator),cell2mat(B(BIterator)))
A is n*6, where the iterator runs on n, and B is a cell with 3*3 doubles (length m). Thanks for the help.
5 个评论
Star Strider
2014-10-26
Please enlighten us!
What do you want to do? (A bit more of your code prior to the line you posted may help explain that.) Include whatever defines ‘AIterator’ and ‘BIterator’
What does ‘function’ do?
If your code is throwing errors, please copy and paste into your Question the complete text of the error messages, and the line that is throwing the error.
UCL student
2014-10-26
UCL student
2014-10-26
Jan
2014-10-26
I'm deeply confused. What is the meaning of:
The function calculates an array based on the 6; and 3*3 doubles.
?? Please post a minimal running example, which demonstrates your problem.
UCL student
2014-10-27
回答(1 个)
Guillaume
2014-10-26
bsxfun can only return matrices, not cell arrays, and only operates on matrices as well. If your output has to be a cell array, you can't use bsxfun.
Without knowing more about function, the best that you could do is vectorise the inner loop with:
outM(AIterator, :) = cellfun(@(b) function(A(AIterator, :), cell2mat(b), B, 'UniformOutput', false);
类别
在 帮助中心 和 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!