Get the last rows of tables inside a cell array
9 views (last 30 days)
Show older comments
Hello,
I want to get the last rows of each table inside a cell array. I try to solve this without a loop because the cell array is quite big.
a = cell(1, 50);
b = rand(780, 6);
a(:) = {table(b(:, 1), b(:, 2), b(:, 3), b(:, 4), b(:, 5), b(:, 6))};
Now in my case the tables inside the cells all have the same column names but different. I tried
c=vertcat(a{:}(end,:))
and different indexing but I can't get it to work.
Intermediate brace '{}' indexing produced a comma-separated list with 50 values, but it must produce a single value when followed by subsequent indexing operations.
How can I solve this?
Thank you very much!
0 Comments
Answers (1)
Matt J
on 18 Mar 2023
Edited: Matt J
on 18 Mar 2023
There is no way to avoid the slow speed of a loop when dealing with cell arrays. Cell arrays are not meant to be fast. However, you can abbreviate the loop syntax by doing,
a = cell(1, 50);
b = rand(780, 6);
a(:) = {table(b(:, 1), b(:, 2), b(:, 3), b(:, 4), b(:, 5), b(:, 6))};
c=cellfun(@(z) z(end,:) ,a,'uni',0)
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!