Number of variables vary in function definition

7 visualizaciones (últimos 30 días)
Lenilein
Lenilein el 5 de Feb. de 2020
Comentada: Lenilein el 5 de Feb. de 2020
Hello,
I tried looking at following problem under all angles but couldn't find a solution: I have a matrix A with n number of rows and p columns. Whereas the number of columns is fixed, the number of rows is dependant on a parameter. I want to concatenate the values of each column to one element and this formula would give me the output that I wish if the number of rows is 5:
X = arrayfun(@(x,y,z,w,v) (strcat(num2str(x),num2str(y),num2str(z),num2str(w),num2str(v))),A(1,:),A(2,:),A(3,:),A(4,:),A(5,:),'un',0);
However, in my case the number of rows varies and therefore the number of variables x,y etc. vary as well.
Do you know a way to automatically adjust the number of variables and the rest of parameters (num2str(x), A(1,:), etc.) accordingly? I suspect this could be possible with a loop but couldn't find the way to do it.
Thanks!

Respuesta aceptada

the cyclist
the cyclist el 5 de Feb. de 2020
Here is a different approach, using a loop and sprintf:
% Count columns and preallocate X
numberCols = size(A,2);
X1 = cell(1,numberCols);
% Fill cell with desired strings
for nc = 1:numberCols
X1{nc} = sprintf('%d',A(:,nc));
end
  1 comentario
Lenilein
Lenilein el 5 de Feb. de 2020
Thank you, it works great + I got to know a new function! :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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