0

I have matrix

A = [ 5 6 7;
      7 5 6 ]

B = [ 1 2 3;
      3 1 2 ]

C = [ 1 0 2;
      0 2 1 ]

Start time A = [ 2 3 7;
                 1 6 8 ]

Start time C = [ 1 0 5;
                 0 4 7 ]

Question : I want to repeat matrix A by using Matrix B. and I also have matrix Start time A that is column of beginning where we have to put each value of Matrix A. and then I have to input value "0" as much as each value of matrix C (value "0" is repeated by matrix C) in column number that is value of matrix "start time C" and before putting value of each matrix A.

so that I have to have output like this below matrix :

Result = [ 0 5 6 6 0 0 7 7 7;
           7 7 7 0 0 5 0 6 6 ]

thanks for your help guys

Amro
  • 123,847
  • 25
  • 243
  • 454
Febri Dwi Laksono
  • 309
  • 1
  • 7
  • 15
  • 1
    Could you provide a step by step of how a single run through is supposed to work? – Ben A. Jul 19 '12 at 16:11
  • @Ben A. : actually i want to make a new matrix which is a combination from matrix A and value "0". In the new matrix, I have to repeat value each element in Matrix A by using Matrix B (so matrix B is matrix which is how many value I have to repeat matrix A). and matrix C is the matrix which shows us how many value "0" that I have to put in matrix result. and then matrix start time A and start time C is the matrix which shows us what column in matrix result we have to put value of matrix A and value "0". did you get what i mean? – Febri Dwi Laksono Jul 20 '12 at 05:07
  • Some related questions: [MATLAB Array Manipulation](http://stackoverflow.com/q/1975772/97160), [Element-wise array replication according to a count](http://stackoverflow.com/q/2382319/97160) – Amro Jul 20 '12 at 13:19

1 Answers1

2

One possible solution is the following. Note that I took the freedom to assign meaningful names to your variables, as names such as A and Start Time A (which is not even a valid Matlab identifier) are really easy to confuse. Also you can see that your matrices C and Start Time C are redundant because all the information is already encoded in A, B and Start Time A.

% The values to put in the result matrix.
value = [5 6 7;
         7 5 6];
% Column index where each sequence starts in the result matrix.
start = [2 3 7;
         1 6 8];
% The length of each sequence, i.e. how often to put the value into the result.
count = [1 2 3;
         3 1 2];

% Determine the longest row. Note: At this place you could also check, if all 
% rows are of the same length. The current implementation pads shorter rows with
% zeros.
max_row_length = max(start(:, end) + count(:, end) - 1);

% Allocate an output matrix filled with zeros. This avoids inserting sequences
% of zeros afterwards.
result = zeros(size(start, 1), max_row_length);

% Finally fill the matrix using a double loop.
for row = 1 : size(start, 1)
    for column = 1 : size(start, 2)
        s = start(row, column);
        c = count(row, column);
        v = value(row, column);
        result(row, s : s + c - 1) = v;
    end
end

The result is

result =

     0     5     6     6     0     0     7     7     7
     7     7     7     0     0     5     0     6     6

as requested.

Mehrwolf
  • 8,208
  • 2
  • 26
  • 38
  • How can I modify your code for the 3d Matrix. Because actually all of above matrix is 3D matrix. I feel difficult to solve it. thanks you – Febri Dwi Laksono Jul 26 '12 at 08:09
  • @Mehworlf : I had added third dimension size in matrix result, and I also had adapted to line result so that it will be result(row,s:s+c-1,third_dimension_size). but after I tried to run, the result is that each value(:,:,1)...val(:,:,size_of_third_dimension) has the same result. so what can I do again Mr. – Febri Dwi Laksono Jul 26 '12 at 11:48
  • @Mehwolf: and the result only represented the last value of matrix value – Febri Dwi Laksono Jul 26 '12 at 11:52
  • You should use the colon if you want to address more than one index simultaneously, e.g. Result(row,s:s+c-r,:) = v. This sets all values along the 3. dimension to v. – Mehrwolf Jul 26 '12 at 12:50
  • @Mehwolf: but i wanna matrix result is that has val(:,:,1)...val(:,:,uk_pop) that has different value depend on matrix value which is also 3D matrix. I had tried already to use colon, but the matrix result is still the same for each val mr. – Febri Dwi Laksono Jul 26 '12 at 13:56
  • max_row_length = max(star_time(:, jum_bag) + ptl(:, jum_bag) - 1); result = zeros(size(star_time, 1), max_row_length,uk_pop); for row = 1 : size(star_time, 1) for column = 1 : size(star_time, 2) s = star_time(row, column,ii); c = ptl(row, column,ii); v = krom(row,column,ii); result(row, s : s + c - 1,ii) = v; end end – Febri Dwi Laksono Jul 26 '12 at 13:56