How to find the minimum number of row and column and concatenate to a matrix
2 views (last 30 days)
Show older comments
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);
5 Comments
Accepted Answer
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 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!