building matrix of a variable series in a for loop

조회 수: 1 (최근 30일)
m naveed
m naveed 2016년 6월 28일
편집: Stephen23 2019년 6월 25일
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
m naveed
m naveed 2016년 7월 5일
never mind...found the solution.
evalin('base', (['y', num2str(i)])); does the job

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2016년 6월 29일
편집: Andrei Bobrov 2016년 6월 29일
a = rand(21);
n = numel(a);
m = ceil(sqrt(n));
out = reshape([a(:);nan(m^2-n,1)],m,[])';

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by