Write multiple function output in one variable

11 visualizaciones (últimos 30 días)
StefSter
StefSter el 17 de Nov. de 2022
Comentada: Voss el 18 de Nov. de 2022
I'm writing a function ind = myfun(N,k) where i need the coordinates of a single index k. My input is k and the number of elements for every dimension in vector N. The Matlab routine ind2sub would work perfectly for my problem, but it writes every coordinate in an extra variable, so I would have to write [ind1, ind2, ...] = ind2sub(N,k) and then ind = [ind1, ind2, ... ]. My problem is that the number of variables is not fixed, it is an input for my function. Is it possible to write the function output of ind2sub not in several variables but in one vector?

Respuesta aceptada

Voss
Voss el 17 de Nov. de 2022
function ind = myfun(N,k)
[temp{1:numel(N)}] = ind2sub(N,k);
ind = [temp{:}];
end
  3 comentarios
StefSter
StefSter el 18 de Nov. de 2022
Thank you very much, that worked perfectly.
Voss
Voss el 18 de Nov. de 2022
Very good point, @the cyclist. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by