create matrix again.....need help...please

1 view (last 30 days)
I have 2 matrices, A(10,10) and B(10,20). which have same size in row and different in column because matrix A consist of 10 variables, which each variables have different size in column. and the column size depend on user input (in matrix), if the input are [1 1 2 1 2 1 1 2 1 1] then matrix A have 13 column. matrix B also consist of 10 variables, column size in matrix B depend on user input either, if the input is 2 then each variables have the same size of column (2), and the total matrix B have 2*10=20 column.
Now,i wanna create matrix C which consist of A and B with the structure like this C=[A(:,1) B(:,1:2) A(:,2) B(:,3:4) A(:,3:4) B(:,5:6) A(:,5) B(:,7:8) A(:,6:7) B(:,9:10) A(:,8) B(:,11:12) A(:,9) B(:,13:14) A(:,10:11) B(:,15:16) A(:,12) B(:,17:18) A(:,13) B(:,19:20)].
this question is rather similar to my question before, but both matrice have the same size before.please help.
thanks in advance.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 11 Nov 2011
A = randi([100 200],10,10);
B = randi(99,10,20);
a = [1 1 2 1 2 1 1 2 1 1];
b = 2;
k1 = arrayfun(@(i1)A(:,i1*ones(a(i1),1)),1:size(A,2),'un',0);
k2 = mat2cell(B,size(B,1),ones(size(B,2)/b,1)*b);
out = cell(1,numel(k1)*2);
out(1:2:end) = k1;
out(2:2:end) = k2;
out = cell2mat(out);
or
out = cell2mat(reshape([k1;k2],1,[]))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!