building matrix of a variable series in a for loop

1 visualización (últimos 30 días)
m naveed
m naveed el 28 de Jun. de 2016
Editada: Stephen23 el 25 de Jun. de 2019
hi all...i have some variables with their values in workspace, lets say y1,y2....y9.
I want to convert this array in to 3 x 3 matrix such that [y1 y2 y3; y4 y5 y6; y7 y8 y9]..automatically. So that next time if i have values like, y1,y2,..........y49, some script automatically builds a matrix [y1,y2...y7; y8,y8....y14;....and so on]
thanks in advance
  5 comentarios
m naveed
m naveed el 5 de Jul. de 2016
never mind...found the solution.
evalin('base', (['y', num2str(i)])); does the job

Iniciar sesión para comentar.

Respuestas (1)

Andrei Bobrov
Andrei Bobrov el 29 de Jun. de 2016
Editada: Andrei Bobrov el 29 de Jun. de 2016
a = rand(21);
n = numel(a);
m = ceil(sqrt(n));
out = reshape([a(:);nan(m^2-n,1)],m,[])';

Categorías

Más información sobre Loops and Conditional Statements 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