Acess all last array elements withtin cells

조회 수: 1 (최근 30일)
Marc Laub
Marc Laub 2022년 10월 25일
댓글: Chunru 2022년 10월 25일
Hey,
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:
precipitate{1}=[1,2,3,4;1,2,3,4];
precipitate{2}=[1,2,3,4,5;1,2,3,4,5];
recipitate{2}=[1,2,3;1,2,3];
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
res=[4,5,3;4,5,3];
process them to like
[5,6,4;5,6,4]
and attach them all at once so that it would look like
precipitate{1}=[1,2,3,4,5;1,2,3,4,5];
precipitate{2}=[1,2,3,4,5,6;1,2,3,4,5,6];
precipitate{2}=[1,2,3,4;1,2,3,4];
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

채택된 답변

Chunru
Chunru 2022년 10월 25일
편집: Chunru 2022년 10월 25일
You can use cellfun:
precipitate{1}=[1,2,3,4;1,2,3,4];
precipitate{2}=[1,2,3,4,5;1,2,3,4,5];
precipitate{3}=[1,2,3;1,2,3];
% get the last column
res = cellfun(@(x) x(:, end), precipitate, 'UniformOutput', false)
res = 1×3 cell array
{2×1 double} {2×1 double} {2×1 double}
% process it
res=cellfun(@(x) x+1, res, 'UniformOutput', false)
res = 1×3 cell array
{2×1 double} {2×1 double} {2×1 double}
% attach it
precipitate = cellfun(@(x, y) [x y], precipitate, res, 'UniformOutput', false);
celldisp(precipitate)
precipitate{1} = 1 2 3 4 5 1 2 3 4 5 precipitate{2} = 1 2 3 4 5 6 1 2 3 4 5 6 precipitate{3} = 1 2 3 4 1 2 3 4
  댓글 수: 4
Marc Laub
Marc Laub 2022년 10월 25일
sry to annoy, not comfotable with cell indexing..
what if in that case
res=cellfun(@(x) x+1, res, 'UniformOutput',
the added 1 is not a contant but a vector, for examle a 1 for the first case, 0.5 for the second...
I only get as res something like {[5,5,5]} {(5.5, 5.5 , 5.5)}... instead of {5} {5.5}...
Chunru
Chunru 2022년 10월 25일
You can perform whatsoever processing you want. Add 1 is just an example you have given.

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

추가 답변 (1개)

Rik
Rik 2022년 10월 25일
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.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by