Annie Sharon
Annie Sharon

Reputation: 17

How to split up the elements in matrix

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

if d=[1 2 3],

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

then using,

d = [0, ceil((d./d(end))*length(x))]; %d=[2 5 9]
disp('The resultant split up is:')
for i=2:length(d)
  disp(x((d(i-1)+1):d(i)));
end

the output has to be, 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

Upvotes: 1

Views: 407

Answers (1)

Eitan T
Eitan T

Reputation: 32920

If I understand your question correctly, then mat2cell is what you need: Here's a short example:

%// Bits and hops array
bits = '10001100';
hops = [3 2 3];

A = mat2cell(bits(:)', 1, hops)

The result is a cell array of strings:

A = 
    '100'    '01'    '100'

This approach works with number arrays as well (e.g bits = [1 0 0 0 1 1 0 0]).

Upvotes: 2

Related Questions