MATLAB: Cell and Mat problem cell matrixmatrixstore A=[1 2;5 2] B=[2 1;4 5] I want this as Ans: C=[1 2 0 0 5 2 0 0 0 0 2 1 0 0 4 5] Actualy I want to store matrixes in a single matrix (as given in Ans) because my prog generate 10 matrix. Best Answer You can use blkdiag:>> A = [1,2;5,2];>> B = [2,1;4,5];>> blkdiag(A,B)ans = 1 2 0 0 5 2 0 0 0 0 2 1 0 0 4 5If you are generating those matrices in a loop, then you can simply put them into a cell array first:>> N = 5;>> C = cell(1,N);>> for k = 1:N; C{k} = randi(9,2); end % <- your loop>> blkdiag(C{:})ans = 1 2 0 0 0 0 0 0 0 0 3 2 0 0 0 0 0 0 0 0 0 0 3 1 0 0 0 0 0 0 0 0 4 9 0 0 0 0 0 0 0 0 0 0 9 5 0 0 0 0 0 0 0 0 5 4 0 0 0 0 0 0 0 0 0 0 9 2 0 0 0 0 0 0 0 0 4 8 0 0 0 0 0 0 0 0 0 0 4 4 0 0 0 0 0 0 0 0 3 1 Related SolutionsMATLAB: How to import .dat data fid=fopen('yourfile.dat','r');data=cell2mat(textscan(fid,'','CollectOutput',1,'CommentStyle','#'));fid=fclose(fid);looks like would do it. You'll end up w/ an array of double. MATLAB: Putting the combinations of a sequence in an cell array >> vec = 1:5;>> fun = @(n)num2cell(combnk(vec,n),2);>> out = arrayfun(fun,1:numel(vec),'uniformoutput',0);>> out = vertcat(out{:});>> out{:}ans = 1ans = 2ans = 3ans = 4ans = 5ans = 1 2ans = 1 3ans = 1 4ans = 1 5ans = 2 3ans = 2 4ans = 2 5ans = 3 4ans = 3 5ans = 4 5ans = 1 2 3ans = 1 2 4ans = 1 2 5ans = 1 3 4ans = 1 3 5ans = 1 4 5ans = 2 3 4ans = 2 3 5ans = 2 4 5ans = 3 4 5ans = 1 2 3 4ans = 1 2 3 5ans = 1 2 4 5ans = 1 3 4 5ans = 2 3 4 5ans = 1 2 3 4 5>> Related QuestionDelete rows and columns with zeros in a multi level cell array matrix.How to use blkdiag in 3D matrix3D matrix summationCreation of matrices (24,72) M1&M2How to create a 2D diagonal matrix from a 3D matrix with a generic dimension? It could be with a loop structure.Cannot get the same result in GA as I get in linprogHow can i solve this problem ? its easy but i dont know!
Best Answer