Acess all last array elements withtin cells
1 view (last 30 days)
Show older comments
Marc Laub on 25 Oct 2022
Commented: Chunru on 25 Oct 2022
I need to store somae data and later acess them again. For further calculation that is done repetedly I would prefer to acces them as fast as possible so that I can use them in a cevtor function. Right know I dont know how to acess them.
They would be either stored in a cell or a class, not sure yet, but the structutre would be similar.
For the cells I would have something like:
in the class, ech element of the class would have a property that keeps that array.
I know need to acess all last colums of the arrays at once, process them and attach new data at the end.
So I would need to acces them so that the result would be
process them to like
and attach them all at once so that it would look like
Right now I am struggling to acces them all at once without a loop.
Does anybody know how to manage that?
Many thanks in advance
Edited: Chunru on 25 Oct 2022
You can use cellfun:
% get the last column
res = cellfun(@(x) x(:, end), precipitate, 'UniformOutput', false)
% process it
res=cellfun(@(x) x+1, res, 'UniformOutput', false)
% attach it
precipitate = cellfun(@(x, y) [x y], precipitate, res, 'UniformOutput', false);
You can perform whatsoever processing you want. Add 1 is just an example you have given.
More Answers (1)
Rik on 25 Oct 2022
Loops are faster than cellfun (excluding the legacy syntax). The reason is that cellfun will hide the loop internally and you have the additional overhead of an anonymous function.
So there is no reason in terms of performance to avoid loops.
The main exception to this is when there exists a vectorized function to do something. Using sum is obviously faster than using plus in a loop.
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!