0

If, for example, I have a 5000x30 matrix and I want to obtain 10 sub matrices having dimensions of 5000x3, how do I code this in Matlab. I have read several post on this issue,this one here for example, but none of them keep the number of rows in the submatrices the same as the main matrix.

As I will be handling very large matrices, I would prefer a code that is faster, like using Matlab's inbuilt functions such as mat2cell or any other vectorized method, but not with loops.

Community
  • 1
  • 1
nashynash
  • 375
  • 2
  • 19
  • 1
    5000 by 30 isn't very big. What's wrong with just using `A(:,1:3)` or `A(:,22:25)` when you need to use parts of a matrix? Your question isn't very clear about what sort of results you want, and how you want to use the resulting matrices. – David Jan 21 '16 at 02:25
  • @David Like I said I will be handling very large matrices, like 500000x300 or higher. `A(:,1:3)` will give me only one sub-matrix. What I want is 10 sub-matrix each having equal dimensions as mentioned above. Each sub-matrix corresponds to a physical behavior and I will using them for further calculations. – nashynash Jan 21 '16 at 02:31
  • 6
    How about `reshape(A, 5000, 3, 10)`? – beaker Jan 21 '16 at 02:48
  • @beaker that seems to work pretty well and fast as well. Thank you. – nashynash Jan 21 '16 at 03:00

2 Answers2

1

As per @beaker comment, using reshape(A, 5000, 3, 10) solved my problem.

Novice_Developer
  • 1,432
  • 2
  • 19
  • 33
nashynash
  • 375
  • 2
  • 19
0
A = rand(5000,30);
b = {}; % Somewhere to store sub-matrices
for k = 1:10
    b{k} = A(:, (k*3-2):(k*3));
end
pancake
  • 3,232
  • 3
  • 22
  • 35