Get the last rows of tables inside a cell array

7 visualizzazioni (ultimi 30 giorni)
Stephan Freitag
Stephan Freitag il 18 Mar 2023
Modificato: Matt J il 18 Mar 2023
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!

Risposte (1)

Matt J
Matt J il 18 Mar 2023
Modificato: Matt J il 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)
c = 1×50 cell array
Columns 1 through 13 {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} Columns 14 through 26 {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} Columns 27 through 39 {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} Columns 40 through 50 {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table} {1×6 table}

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by