hi
I have an empty cell array
Let call it W
At the output It must have a cell array with two columns and n rows
Each element of columns is an array with variable length
I wanna write at the end of arrays in columns
How can access the last element of that arrays in the cells
for example W(1,2) end+1 i.e At the end of an array at the second column of first row

 採用された回答

fereshteh beygi ahmadvandi
fereshteh beygi ahmadvandi 2021 年 3 月 16 日
編集済み: fereshteh beygi ahmadvandi 2021 年 3 月 16 日

0 投票

W{1,2}(1,end+1)
But first, define your empty cell array as follows:
W = cell(2,2);

4 件のコメント

Adam Danz
Adam Danz 2021 年 3 月 16 日
How does that differ from the existing answer below that suggests this same method? Your question stated that the empty cell array was already defined.
Note, that if you have a variable that stores all of the new end-point-values to be added to each cell element, you can do that in 1 line of code, also shown in my answer.
fereshteh beygi ahmadvandi
fereshteh beygi ahmadvandi 2021 年 3 月 16 日
Thank you 🌹🌹🌹🌹🌹
I did not see your answer By the way, when you answered me, I had found the solution myself
I said that I wanna write at the end of that arrays i.e append element
I have to write the code as I said, otherwise it gives an error and does not have the correct output.
fereshteh beygi ahmadvandi
fereshteh beygi ahmadvandi 2021 年 3 月 16 日

I wish I could ask you for help with any questions I may have in the future Of course, from a faster method other than this website

Adam Danz
Adam Danz 2021 年 3 月 16 日
> I had found the solution myself
That's the best way to learn! Nicely done.

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2021 年 3 月 16 日
編集済み: Adam Danz 2021 年 3 月 16 日

0 投票

To access the last element of an array stored in cell array C at location {m,n},
y = C{m,n}(end);
To access the last element of all cells stored in cell array C,
y = cellfun(@(x)x(end),C);
Update
To append a value to the end of the vector stored in cell array C at location {m,n},
C{m,n}(end+1) = x;
To append a value M(m,n) in matrix M to the end of the vector stored in cell C{m,n},
C = reshape(arrayfun(@(i){[C{i}, M(i)]},1:numel(C)),size(C));
Demo:
C = reshape(arrayfun(@(i){rand(1,randi(100))},1:100),50,2); % original values
M = reshape(1:100,50,2); % values to append
C2 = reshape(arrayfun(@(i){[C{i}, M(i)]},1:numel(C)),size(C));

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by