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

5 views (last 30 days)
Kong on 29 Mar 2020
Commented: Kong on 30 Mar 2020
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);
Kong on 30 Mar 2020
Can I get some idea to solve this again?

Image Analyst on 29 Mar 2020
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
##### 2 CommentsShowHide 1 older comment
Kong on 29 Mar 2020
How can I use this kind of code to Phi_NuMax{i}?
% Find the minumul number of row and column
row = cellfun(@(x) size(x,1),c );
col = cellfun(@(x) size(x,2),c );
minRow = min(row );
minCol = min(col);
% Adjust the size of each array to minRow-by-minCol
c = cellfun(@(x) x(1:minRow,1:minCol),c,'UniformOutput',false);