0

Hin everyone..I have matrix

col = [1 2 3 9 10 15 16 17]

I need to divide A into 3 with length B = [3 2 3], result required :

col1 = [1 2 3] -> from col(1:3)
col2 = [9 10] -> from col(4:5)
col3 = [15 16 17] -> from col(6:8)

Thank you so much...

2 Answers 2

2

mat2cell can be use:

A = [1 2 3 9 10 15 16 17];
B = [3 2 3];
mat2cell(A,1, B)

Result:

{
  [1,1] =

     1   2   3

  [1,2] =

      9   10

  [1,3] =

     15   16   17

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @rahnema1
@rahnema1 nice. +1
1

I assume that lenghts in B match col length, so every element is accounted for. If so, you can do it using simple for loop as follows:

col = [1 2 3 9 10 15 16 17];

B = [3 2 3];

start_idx = 1;
for b = B
    col_part = col(start_idx : start_idx+b-1)
    start_idx = start_idx+b;
end

Results in:

col_part =

     1     2     3


col_part =

     9    10


col_part =

    15    16    17

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.