Generate a comma-separated list from a numeric array?

113 visualizaciones (últimos 30 días)
Eric Sampson
Eric Sampson el 20 de Mzo. de 2013
Comentada: FM el 23 de Oct. de 2022
Is there a way to generate a comma-separated list from a numeric array without generating a temporary cell array? If not, would you find this functionality useful if it was added to MATLAB?
I know I would - the times that I've wanted to use this construct are very similar to the times that you use {:} with a cell array. For instance, you may have a set of n values in an array A that you want to pass to a function that takes varargin arguments. Right now if you had a cell array C you could do func( C{:} ), but you can't do func( A(someSyntax) ) . Similarly sometimes you want to be able to generate a CSL as an output argument list. Also, sometimes you might want to do B = {1, 2, C{:}, 'foo'}, which is different than B = {1, 2, A(:), 'foo'}. Lastly, you can't do things like a = 3; b=2; c=1; lol = [1 2 3]; [a b c] = deal( lol(someSyntax) )
  6 comentarios
Matt J
Matt J el 3 de Jul. de 2017
Editada: Matt J el 3 de Jul. de 2017
[arr.field]=deal(0)
requires no special code. However, striving for efficiency with structure and cell arrays is bound to be unfruitful. Structs and cells are inherently inefficient for storing large data, as compared to vectors and matrices, and you shouldn't really be using them in such situations. Memory access will be slow for one thing because they do not store data contiguously in RAM. If on the other hand, you don't have large data, efficiency isn't really beneficial.
Lachlan
Lachlan el 24 de Ag. de 2017
Thanks, Matt J. I hadn't noticed deal() could be used with a scalar.
I agree that matrices are far and away the most efficient where they can be used, but I was assuming that (memory inefficiency) < (memory inefficiency + interpreter inefficiency).

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 20 de Mzo. de 2013
[a b c] = deal( lol(someSyntax) )
This one you can do, and with even easier syntax
>> [a b c] = vout(lol)
a =
1
b =
2
c =
3
where vout() is defined below. You can't do it without creating a temporary cell array, but since this is hidden inside vout(), I don't see why it would matter.
function varargout=vout(C)
if isnumeric(C), C=num2cell(C); end
C=C(:).';
varargout=C(1:nargout);
  7 comentarios
Matt J
Matt J el 28 de En. de 2017
Editada: Matt J el 29 de En. de 2017
The operator .' is MATLAB's transpose operator,
It just acts here to transpose a column cell array into a row cell array.
FM
FM el 23 de Oct. de 2022
Thanks.
I find myself searching for this function again in 2022. It sure would be nice if it can be made native to Matlab. Having an extra function means one more thing that can go wrong if you don't properly sync your library of utility functions between computers, and another dependency to carry when deploying. Additionally, when posting minimal working examples online, it's more code that needs to be carried.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by