2

How to repeat

A = [ 1 2 ; 
      3 4 ]

repeated by

B = [ 1 2 ; 
      2 1 ]

So I want my answer like matrix C:

C = [ 1 2 2; 
      3 3 4 ]

Thanks for your help.

Amro
  • 123,847
  • 25
  • 243
  • 454
Febri Dwi Laksono
  • 309
  • 1
  • 7
  • 15

2 Answers2

6

Just for the fun of it, another solution making use of arrayfun:

res = cell2mat(arrayfun(@(a,b) ones(b,1).*a, A', B', 'uniformoutput', false))'

This results in:

res =

     1     2     2
     3     3     4
H.Muster
  • 9,297
  • 1
  • 35
  • 46
5

To make this simple, I assume that you're only going to add more columns, and that you've checked that you have the same number of columns for each row.

Then it becomes a simple combination of repeating elements and reshaping.

EDIT I've modified the code so that it also works if A and B are 3D arrays.

%# get the number of rows from A, transpose both
%# A and B so that linear indexing works
[nRowsA,~,nValsA] = size(A);
A = permute(A,[2 1 3]);
B = permute(B,[2 1 3]);

%# create an index vector from B 
%# so that we know what to repeat
nRep = sum(B(:));

repIdx = zeros(1,nRep);
repIdxIdx = cumsum([1 B(1:end-1)]);
repIdx(repIdxIdx) = 1;
repIdx = cumsum(repIdx);

%# assemble the array C
C = A(repIdx);
C = permute(reshape(C,[],nRowsA,nValsA),[2 1 3]);

C =
     1     2     2
     3     3     4
Jonas
  • 74,690
  • 10
  • 137
  • 177
  • The +1 is for understanding the cryptic logic of the question :) – Eitan T Jul 16 '12 at 13:48
  • 2
    @EitanT: I saw that there was "repeat" in the title, and that we should use B to find out how to repeat elements from A. I actually found it pretty obvious. Thanks for the upvote, anyway :) – Jonas Jul 16 '12 at 13:49
  • @jonas : how if i wanna do like with more than value of matrix A and B? – Febri Dwi Laksono Jul 17 '12 at 06:30
  • @FebriDwiLaksono: I don't quite understand. Can you make an example, please? – Jonas Jul 17 '12 at 11:49
  • @jonas: i mean that, actually Matrix A have more than one val, example (val (;,;,1), val (;,;,2),...). matrix B also have more than one val. so I felt difficult when I used your logic, specially in cumsum([1 B(1:end-1)]);. do you understrand jonas?? – Febri Dwi Laksono Jul 17 '12 at 16:24
  • @FebriDwiLaksono: The code works for nd-arrays with a few modifications. See my edited code – Jonas Jul 17 '12 at 19:03
  • @jonas: thank you so much jonas for your help. ok I will try to use your code. and if I have any question, I will ask you again. :) – Febri Dwi Laksono Jul 18 '12 at 04:08
  • @jonas: jonasss, thanks you so much for your code. it helped me so much!!. jonas, can i know your email?? so that i can ask you anytime if I want?? – Febri Dwi Laksono Jul 18 '12 at 04:49
  • @FebriDwiLaksono: If you found my answer helpful, please consider upvoting/accepting it. Also, I suggest you ask your questions on StackOverflow, since it is much more likely that they get answered in a timely fashion. – Jonas Jul 18 '12 at 11:37
  • @jonas: thanks you very much jonas. and maybe after this, I will ask you again Mr. Jonas. So I hope You will give your idea as soon as possible yah. hehe – Febri Dwi Laksono Jul 19 '12 at 07:03