MATLAB: How to find the minimum number of row and column and concatenate to a matrix

csvmatrixmatrix arraymatrix manipulationvectorvectorizationvectors

Hello.
When I used this code, I got the error.
This is because the cell of Phi_NuMax has different matrix.
Could you explain how to Find the minimum number of rows and column and concatenate it?
1 cell : 100 x 144
2 cell : 95 x 144
…….
I want to match the matrix as same size of rows and column.
Phi_NuMax{i} = (U{i}(:, 1:r{i})*(S{i}(1:r{i}, 1:r{i}).^(1/2)))';
% How to Find the minimum number of row and column of Phi_NuMax{i} to concatenate it ?
% Each Phi_NuMax{i} has different size of rows and columns
% Each data{i} has same size of rows and columns
Y{i} = Phi_NuMax{i} * data{i};
Y{i} = reshape(Y{i},[],1);
end
K = cell2mat(Y);

Best Answer

Don't you need the minimum dimension in order to concatenate? In other words, you need to find the max dimensions. Untested code:
minRequiredRows = 0;
minRequiredColumns = 0;
for k = 1 : length(Phi_NuMax)
thisMatrix = Phi_NuMax{k};
[thisRows, thisColumns] = size(thisMatrix);
fprintf('Cell #%d has %d rows and %d columns.\n', k, thisRows, thisColumns);
% Find out how wide the matrix will need to be if they are all stacked on top of one another.
minRequiredColumns = max(minRequiredColumns, thisColumns);
% Sum up how many rows we'll need to hold all of them vertically.
minRequiredRows = minRequiredRows + thisRows;
end
fprintf('At a minimum, you will require %d rows and %d columns.\n', k, minRequiredRows, minRequiredColumns);
% Now instantiate a matrix for all the individual matrices stitched together vertically.
allMatrixes = zeros(minRequiredRows, minRequiredColumns);
% Now concatenate by inserting the cell into all Matrixes at the appropriate row number.
currentRow = 1;
for k = 1 : length(Phi_NuMax)
thisMatrix = Phi_NuMax{k};
[thisRows, thisColumns] = size(thisMatrix);
fprintf('Inserting cell #%d with %d rows and %d columns.\n', k, thisRows, thisColumns);
allMatrixes(currentRow : currentRow + thisRows - 1, 1:thisRows) = thisMatrix;
% Move the pointer to the next place where we will insert the next matrix.
currentRow = currentRow + thisRows;
end