SIMPLE QUESTION : Partiation of data

8 views (last 30 days)
Aydin Ahmadli
Aydin Ahmadli on 17 Nov 2018
Commented: Image Analyst on 17 Nov 2018
Hello, I have 2x600 matrix. I need to separate it into 5 matrices, each of which is 2x120.... Which way is the easiest way to do that?
  1 Comment
Guillaume
Guillaume on 17 Nov 2018
As per John's answer, the easiest way to do it is not to do it, particularly as it's never necessary.
Creating more variables than you started with is rarely a good idea.
Whatever you do, do not create numbered variables, which are always a clear indication that you should have the lot stored in a single container (ie. what you started with).

Sign in to comment.

Answers (3)

John D'Errico
John D'Errico on 17 Nov 2018
Edited: John D'Errico on 17 Nov 2018
No. You THINK you need to turn it into 5 matrices. That is because you can't see a better solution. Instead, turn it into 5 planes, of a 3 dimensional matrix.
So create ONE 3-d matrix, of shape 2x120x5. I don't know what your data means. But if you are willing to take the first 120 elements of each column and make that into one matrix, then just do it as:
B = reshape(A,[2,120,5]);
Now, access the k'th such submatrices using B(:,:,k). So B(:,:,1) is the first submatrix of 120 columns.
For example, we can see how this works for a small example. I'll start with a 2x15 array, then convert it to a matrix of 5 2x3 arrays.
A = rand(2,15)
A =
Columns 1 through 7
0.744327416657354 0.297971773455507 0.719478432476681 0.151985104556931 0.452444444656766 0.402740713882364 0.295662752964438
0.291378756948867 0.173484833804965 0.552763600208399 0.450761729584393 0.58580839412901 0.552216136944867 0.858665405591957
Columns 8 through 14
0.505786947631755 0.984645656346216 0.465556275060997 0.853640914496005 0.674128925703711 0.0378391148451244 0.989458188345198
0.237584178910651 0.470371692218976 0.370847305045254 0.309872751858736 0.144557058974711 0.0659471935175786 0.13582936000526
Column 15
0.346942833360261
0.0442698779049502
B = reshape(A,[2,3,5])
B(:,:,1) =
0.744327416657354 0.297971773455507 0.719478432476681
0.291378756948867 0.173484833804965 0.552763600208399
B(:,:,2) =
0.151985104556931 0.452444444656766 0.402740713882364
0.450761729584393 0.58580839412901 0.552216136944867
B(:,:,3) =
0.295662752964438 0.505786947631755 0.984645656346216
0.858665405591957 0.237584178910651 0.470371692218976
B(:,:,4) =
0.465556275060997 0.853640914496005 0.674128925703711
0.370847305045254 0.309872751858736 0.144557058974711
B(:,:,5) =
0.0378391148451244 0.989458188345198 0.346942833360261
0.0659471935175786 0.13582936000526 0.0442698779049502
So in the future, if you had many such arrays, everything is easy to work with. You can easily write code to reference any of the submatrices. If you created separate arrays, then your next question would be how do you access all of those variables with numbers in the names.
One smiple alternative is to use mat2cell, creating cell arrays. This is arguably easier to access any such sub-matrix, but you need to learn to use cell arrays. Not difficult, and it still requires only one line of code.
B = mat2cell(A,2,repmat(3,1,5))
B =
1×5 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double}
B{1}
ans =
0.744327416657354 0.297971773455507 0.719478432476681
0.291378756948867 0.173484833804965 0.552763600208399
As you see, now we can access the first such subarray as just B{1}, using curly braces. B{k} will be the k'th such array.

Image Analyst
Image Analyst on 17 Nov 2018
Try this, where "data" is your array:
data1 = data(:, 1:120);
data2 = data(:, 121:240);
data3 = data(:, 241:360);
data4 = data(:, 361:480);
data5 = data(:, 481:end);
  4 Comments
Guillaume
Guillaume on 17 Nov 2018
Come on! You know that there is often a gulf between what they ask and what they need. Teach them the proper way to use matlab.
Case in point, you've given him 5 separate arrays. So what is the next question: how do I loop over these arrays
Image Analyst
Image Analyst on 17 Nov 2018
That is sometimes true, though not always. And how few arrays do you need to have to not bother with loops or mat2cell()? What if there was an N by 2 array of x,y coordinates and the programmer wanted two arrays out of that, x and y, for convenience in calling plot(), polyfit(), interp1() or whatever. I'd see no harm in breaking that apart into two arrays for simplicity and convenience:
x = xy(:, 1);
y = xy(:, 2);
I don't even see a problem in doing that if you wanted x, y, and z (three arrays). Of course I wouldn't recommend that if there were 20 arrays that needed to be created. So what's the dividing line between small enough, and too large? The user gave very specific, fixed numbers, not some sizes that might be variable or need to be generalized, and I felt that 5 was small enough.

Sign in to comment.


TADA
TADA on 17 Nov 2018
Edited: TADA on 17 Nov 2018
A = Rand(2,600);
There's The Simplest
A1 = A(1:2,1:120);
A2 = A(1:2,121:240);
% And So On...
You Can Automate With A Loop:
B = cell(1,5);
newSize = 120;
n=size(A,2)/newSize;
for i =0:n-1
B{I}=A(1:2,i*newSize+1 :(i*newSize)+newSize );
end
That Generates Cell Array Of The Matrices You Wanted. You Can Access Them Like That
B{i} % Where i Is The Index 1..5
And In My Opinion Your Beat Option:
B = mat2cell(A,2,ones(1,n)*newSize );

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!