Writing inside cell array

1 visualización (últimos 30 días)
Matteo Tesori
Matteo Tesori el 17 de Mayo de 2023
Respondida: Sulaymon Eshkabilov el 17 de Mayo de 2023
Let "f" be a function that returns a 3 by 1 cell. I have to evaluate "f" multiple times, say "N", and I would like to store the results in just one 3 by 2 cell "y".
My solution is the following and is based on a temporary 3 by 1 cell "ytemp". Is there a way to obtain the same result without involving "ytemp"?
y = cell(3, N);
for i = 1 : N
ytemp = f(i);
y(:, i) = ytemp(:);
end

Respuesta aceptada

Jon
Jon el 17 de Mayo de 2023
Is this what you are asking for?
y = cell(3, N);
for i = 1 : N
y(:, i) = f(i);
end
  1 comentario
Matteo Tesori
Matteo Tesori el 17 de Mayo de 2023
yes thank you! works like a charm!

Iniciar sesión para comentar.

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 17 de Mayo de 2023
If understood correctly your question, here is how it can be solved:
t = randn(1);
g = sin(2*pi*t);
f = repmat(g, 1,100)+rand(size(t));
N = 100;
y = cell(1,N);
for i = 1 : N
y{1,i} = f(i);
end
y
y = 1×100 cell array
Columns 1 through 13 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 14 through 26 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 27 through 39 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 40 through 52 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 53 through 65 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 66 through 78 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 79 through 91 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} Columns 92 through 100 {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]} {[-0.6299]}

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by