How to reshape matrix

12 views (last 30 days)
Andreja Vujanac
Andreja Vujanac on 2 Aug 2018
Commented: Andreja Vujanac on 5 Aug 2018
Hi everybody, I have matrix which consist of 2340 (results for 26 subject x 90 variable value for each subject) rows and 5 columns (different variables for the same subjects of experiment) and my problem is to resize this matrix by extracting first 45 rows subset of each subject (45x26) in one column,while second half of 45 rows for each subject should be in the next column. I would really appreciate any help.
  1 Comment
Adam Danz
Adam Danz on 2 Aug 2018
Edited: Adam Danz on 2 Aug 2018
Your matrix is 2340 x 5. Then you say you want to extract the first half of data from each subject which would be size 45x26. You lose me there. Wouldn't it be 45x8 for each subject resulting in 1170x8 matrix? Also how can you put 1170x8 matrix into 1 column of another matrix? Please provide an example and/or clearer description.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 2 Aug 2018
Edited: Adam Danz on 2 Aug 2018
Your goal isn't clear (see comment under your question). But I think I understand what you're looking for.
Here I make the following assumptions
  • You have a variable that identifies the subject for each row of your data.
  • The rows of your 2340x5 matrix is organized by subject. So, the first 90 rows are s1, then 90 rows of s2, etc.
  • The number of variables (in your case, 90) will always be even. If this is untrue, you'll have to use a cell array rather than a matrix and the two columns will be uneven.
  • You want to extract data from 1 column at a time. ( This one was really unclear but since you want two columns in a matrix, this has to be the case).
If this is correct, you are not resizing the matrix (ie, using resize()), you are extracting a subset of data to create a new, smaller matrix.
Here I create fake data that fit your description and in 3 lines, I build the 2-column matrix extracting data from column 3 of your data.
% create fake data
nSubj = 26; %number of subjects
nVars = 90; %number of variables
data = rand(nSubj * nVars, 5); %your data (fake)
subjID = meshgrid(1:nSubj, 1:nVars);
subjID = subjID(:); %identifies the subject # for each row of data
% create index that chooses rows from first 1/2 of each subject
firstHalfIdx = repmat([true,false], floor(nVars/2), 1); %floor() in case nVars isn't even.
firstHalfIdx = repmat(firstHalfIdx(:), nSubj, 1);
% Put data into new matrix
col = 3; %choose column to extract
dataHalves = [data(firstHalfIdx, col), data(~firstHalfIdx, col)];
dataHalves is a [1170-by-2] matrix where column 1 is the first half of data from all subjects and column 2 is the 2nd half - extracting data from column 3 of your data.
  2 Comments
Adam Danz
Adam Danz on 2 Aug 2018
Edited: Adam Danz on 2 Aug 2018
The following line will identify the subject number in the dataHalves matrix (rows).
subjHalves = subjID(firstHalfIdx);
Andreja Vujanac
Andreja Vujanac on 5 Aug 2018
Thank you very much

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!