How to convert an array into matrix of different sized cloumns

i want to convert an array just like this suppose A is my array
A=[1 3 5 5 7 8 9 34 34 45 67 78 43 32 23] and i want to convert it into a matrix b which has random number of rows in each column(suppose n number of columns) b=[1 3 5 5 7; 8 9 ;34 34 45 67 78 43 32; 23]

 Accepted Answer

A matrix, by definition, has the same number of elements in each column, and the same number of elements in each row.
Two possible ways to do something like what you want:
  1. pad the columns of a matrix with NaNs so that the number of valid elements in each column can vary
  2. use a cell array of vectors
EDIT: code for one possible solution added 12 Nov 2011
There follows code for one approach to doing this. Note that your example result suggests that the values occupy the first elements of each row, so the NaN are all in the higher columns. I've assumed this is how the result should be.
You haven't explained how to decide how many rows there should be, or alternatively what the distribution of the number of values in each row should be. I've assumed that the number of rows is given, that there should be at least one value in each row, and that the points at which the input is divided can be selected as the first few points of a random permutation of the indexes into the vector.
That gives me this code:
A=[1 3 5 5 7 8 9 34 34 45 67 78 43 32 23]; % data
nrows = 4; % number of rows required in result
nin = length(A);
p = randperm(nin); % nrandom permutation of indexes
starts = [sort(p(1:nrows)) nin+1]; % random set of start points for sections
lengths = diff(starts); % number of values in each row
ncols = max(lengths);
result = NaN(nrows, ncols); % preallocate results matrix
% copy from input vector into successive rows of result
for row = 1:nrows
result(row, 1:lengths(row)) = A(starts(row):starts(row+1)-1);
end
disp(result)

3 Comments

yes, i wan use Nan over there......but plese tell me how to do it....if there is a inbuilt function or not...to do this....or i have made a small loop to do this....see this...
clear all
clc
cl=5;
n=100;
numb=rand(1,100)*100;
i=1;
while(i<=cl)
grp=ceil(rand*n);
coldim(i)=grp;
i=i+1;
if(i==cl && sum(cold)||100)
i=1;
n=100;
end
n=n-grp;
end
sum(coldim)
here cold contains the number of rows in each column(using as cl)
but this program goes into infinite loop
I can see roughly how your code is meant to work, but there's quite a lot wrong and I can't make sense of all of it. Run it step by step using the debugger and see what the variables do to understand how to avoid the infinite loop.
I'd do it rather differently, and I'll put my code into my answer for reference.

Sign in to comment.

More Answers (1)

Hi, you would have to do this as a cell array unless you were content to fill out the "missing" elements in the rows with NaN or zeros.

1 Comment

yes, i wan use Nan over there......but plese tell me how to do it....if there is a inbuilt function or not...to do this....or i have made a small loop to do this....see this...
clear all
clc
cl=5;
n=100;
numb=rand(1,100)*100;
i=1;
while(i<=cl)
grp=ceil(rand*n);
coldim(i)=grp;
i=i+1;
if(i==cl && sum(cold)||100)
i=1;
n=100;
end
n=n-grp;
end
sum(coldim)
here cold contains the number of rows in each column(using as cl)
but this program goes into infinite loop

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!