MATLAB: How to reshape matrix

matrix problem

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 (45×26) in one column,while second half of 45 rows for each subject should be in the next column. I would really appreciate any help.

Best Answer

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.