1
votes

I have a matrix A

 1     1     0     0
 0     1     0     0
 1     0     0     1
 0     0     1     0
 0     0     0     0
 0     1     1     1
 1     1     0     0
 1     0     0     0
 0     0     0     1

I want this matrix to be split according to user's input say d = [1 2 3].

for i=2:length(d)
  d(i) = d(i) + d(i-1); % d = [1 3 6]
end

This gives d = [1 (1+2) (1+2+3)] = d[1 3 6]. There are 9 rows in this matrix, calculate ceil of [(1/6)*9], [(3/6)*9] and [(6/6)*9]. Hence this gives [2 5 9]. First split up is first two rows , 2nd split up is next (5-2=3) 3 rows and third split is (9-5=4) 4 rows.

The output should be like:

The split up is: 1st split up->

      1     1     0     0    % first 2 rows in matrix A
      0     1     0     0

2nd split up->

      1     0     0     1    % next 3 rows
      0     0     1     0
      0     0     0     0

3rd split up->

      0     1     1     1    % next 4 rows
      1     1     0     0
      1     0     0     0
      0     0     0     1
1
Sounds like you've got something that works already. What's the problem/question?tmpearce
do you want this to be done efficiently?Autonomous
@gevang- i would like to display the output in command window. is that possible?durga
just type the entry you want to display, i.e. B{1}, B{2}, B{3}, or all of them B{:}.gevang
i want it automatically displayed on the command window when i run the program.durga

1 Answers

5
votes

You can use mat2cell with input d = [1 2 3] to store the final splits in separate cell arrays

B = mat2cell(A, d+1, size(A,2));

or, to adapt it to your computation of the split row sizes:

d = [1 2 3];
c = cumsum(d); % [1, 3, 6]

s = ceil(size(A,1)*c/c(end)); % [2, 5, 9] 
n = [s(1) diff(s)]; % [2, 3, 4]

B = mat2cell(A, n, size(A,2));

To display the splits you can add a command similar to:

cellfun(@disp, B)