From: Lynn MacDonald on 3 Jul 2010 13:08 Hi, Is there any simple way in MATLAB to take a cell array of numbers and assign the contents of each cell to a matrix position in a preallocated matrix in a patterned fashion? Or, alternatively, are there commands to manipulate individual rows and colums of a matrix? Here is an example of what I would like it to be able to do: Cell array 1: [01][02][03] [04][05][06] Is created in a loop, and then its contents are dumped into a preallocated matrix in this fashion... 01 0 0 02 0 0 03 0 0 04 0 0 05 0 0 06 0 0 On the next iteration of the loop Cell array 2: [07][08][09] [10][11][12] is created and then its contents are dumped into the matrix to get 01 07 0 02 08 0 03 09 0 04 10 0 05 11 0 06 12 0 And in case you aren't getting the pattern, Cell array 3: [13][14][15] [16][17][18] Gets dumped into the matrix to give 01 07 13 02 08 14 03 09 15 04 10 16 05 11 17 06 12 18 Of course this is just an example and the cell arrays I'm working with contain sevel hundred data points and the final matrix is about 2000x2000. I think I can figure out to do it using loops, but the reason I created the cell arrays was to avoid using loops in order to save time when doing math. Is there any quick way to assign the data from the cells to a matrix in a non-contiguous fashion? I know about cell2mat, and can create a multidimensional array of the matrices created, but I don't know how to manipulate them from there. In my mind it is like trying to flatten a rubix cube. I also have to insert rows of data, but if I get an idea of what I should do here, if it is possible, I'm sure I can figure something out.
From: James Tursa on 3 Jul 2010 19:01 "Lynn MacDonald" <willowsword(a)gmail.com> wrote in message <i0nqpj$u1$1(a)fred.mathworks.com>... > Hi, > Is there any simple way in MATLAB to take a cell array of numbers and assign the contents of each cell to a matrix position in a preallocated matrix in a patterned fashion? Or, alternatively, are there commands to manipulate individual rows and colums of a matrix? Yes, but it takes some overhead (data copying and/or for loops). > Here is an example of what I would like it to be able to do: > > Cell array 1: > [01][02][03] > [04][05][06] > > Is created in a loop, and then its contents are dumped into a preallocated matrix in this fashion... > 01 0 0 02 0 0 03 0 0 > 04 0 0 05 0 0 06 0 0 > > On the next iteration of the loop > Cell array 2: > [07][08][09] > [10][11][12] > is created and then its contents are dumped into the matrix to get > 01 07 0 02 08 0 03 09 0 > 04 10 0 05 11 0 06 12 0 > > And in case you aren't getting the pattern, > > Cell array 3: > [13][14][15] > [16][17][18] > > Gets dumped into the matrix to give > 01 07 13 02 08 14 03 09 15 > 04 10 16 05 11 17 06 12 18 e.g., the above example could be done like this: >> A = zeros(2,9) A = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> c1 = rand(2,3) c1 = 0.9501 0.6068 0.8913 0.2311 0.4860 0.7621 >> c2 = rand(2,3) c2 = 0.4565 0.8214 0.6154 0.0185 0.4447 0.7919 >> c3 = rand(2,3) c3 = 0.9218 0.1763 0.9355 0.7382 0.4057 0.9169 >> A(:,1:3:end) = c1 A = 0.9501 0 0 0.6068 0 0 0.8913 0 0 0.2311 0 0 0.4860 0 0 0.7621 0 0 >> A(:,2:3:end) = c2 A = 0.9501 0.4565 0 0.6068 0.8214 0 0.8913 0.6154 0 0.2311 0.0185 0 0.4860 0.4447 0 0.7621 0.7919 0 >> A(:,3:3:end) = c3 A = 0.9501 0.4565 0.9218 0.6068 0.8214 0.1763 0.8913 0.6154 0.9355 0.2311 0.0185 0.7382 0.4860 0.4447 0.4057 0.7621 0.7919 0.9169 Where c1, c2, c3 are the results of a cell2mat operation. But this requires two data copies, one to create the c1 etc and then one to assign the data into A. To avoid the extra data copy, you could assign each cell individually inside a for loop, but then you get the overhead of the for loop. The only way I know of to avoid the overhead of both of these solutions is to resort to a custom mex routine to get direct access to the cell contents and copy directly into the appropriate A positions. James Tursa
From: us on 3 Jul 2010 19:58 "Lynn MacDonald" <willowsword(a)gmail.com> wrote in message <i0nqpj$u1$1(a)fred.mathworks.com>... > Hi, > Is there any simple way in MATLAB to take a cell array of numbers and assign the contents of each cell to a matrix position in a preallocated matrix in a patterned fashion? Or, alternatively, are there commands to manipulate individual rows and colums of a matrix? Here is an example of what I would like it to be able to do: > > Cell array 1: > [01][02][03] > [04][05][06] > > Is created in a loop, and then its contents are dumped into a preallocated matrix in this fashion... > 01 0 0 02 0 0 03 0 0 > 04 0 0 05 0 0 06 0 0 > > On the next iteration of the loop > Cell array 2: > [07][08][09] > [10][11][12] > is created and then its contents are dumped into the matrix to get > 01 07 0 02 08 0 03 09 0 > 04 10 0 05 11 0 06 12 0 > > And in case you aren't getting the pattern, > > Cell array 3: > [13][14][15] > [16][17][18] > > Gets dumped into the matrix to give > 01 07 13 02 08 14 03 09 15 > 04 10 16 05 11 17 06 12 18 > > Of course this is just an example and the cell arrays I'm working with contain sevel hundred data points and the final matrix is about 2000x2000. I think I can figure out to do it using loops, but the reason I created the cell arrays was to avoid using loops in order to save time when doing math. Is there any quick way to assign the data from the cells to a matrix in a non-contiguous fashion? I know about cell2mat, and can create a multidimensional array of the matrices created, but I don't know how to manipulate them from there. In my mind it is like trying to flatten a rubix cube. > > I also have to insert rows of data, but if I get an idea of what I should do here, if it is possible, I'm sure I can figure something out. one of the solutions % the data nc=3; % <- #cols/CELL sample... nr=2; % <- #rows/CELL sample... n=3; % <- #CELL samples... m=nr*nc; cc=cell(n,1); mc=@(x,y) num2cell(reshape(x:y,[],nr).'); % <- dummmy CELL creator macro... % - create your CELLS for i=1:n c=mc(1+(i-1)*m,1+(i-1)*m+m-1); % <- create your CELLs... cc{i,1}=c; % <- collect your CELLs... end % the engine r=cell2mat(reshape(reshape(permute(cat(3,cc{:}),[1,3,2]),m,[]),nr,[])); % the result disp(r); %{ 1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18 %} us
From: Lynn MacDonald on 6 Jul 2010 10:03 Thanks for the suggestions.
|
Pages: 1 Prev: getsampleusingtime method gives an error Next: Intersecting circles |