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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Kong
Kong il 30 Mar 2020
Modificato: Kong il 30 Mar 2020
Thank you so much.
I got this error. I think I have to find minimum size of rows.
clear all
close all
%// read the video:
list = dir('*.avi')
% loop through the filenames in the list
for k = 1:length(list)
reader = VideoReader(list(k).name);
vid = {};
while hasFrame(reader)
vid{end+1} = im2single(readFrame(reader));
end
%// simple background estimation using mean:
bg = csvread('background.csv');
bg = reshape(bg, 144, 180, 3);
%// estimate foreground as deviation from estimated background:
for i=1:3
fIdx(i) = i; %// do it for frame 1 ~ 60
fg{i} = sum( abs( vid{fIdx(i)} - bg ), 3 );
fgh{i} = imresize(fg{i}, 1);
data{i} = fgh{i};
data{i} = double(data{i});
n = size(data{i}, 1);
opt.init_num_secants = n;
idx1{i} = random('unid',size(data{i}, 2), opt.init_num_secants, 1);
idx2{i} = random('unid',size(data{i}, 2), opt.init_num_secants, 1);
secants{i} = data{i}(:, idx1{i})-data{i}(:, idx2{i});
D1{i} = abs(secants{i});
numSecants = size(data{i},1);
options.landmarks = 1:n;
[geo{i}] = IsomapII(D1{i}, 'k', 3, options);
secants{i} = geo{i}/norm(geo{i},2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%Parameter setting
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
opt.outer_iterations = 1000;
switch 'l-bfgs'
case 'grad'
opt.linear_solver = 'grad';
opt.tau = 1e-1; % gradient step size
opt.inner_iterations = 10;
opt.beta1 = 1e-1; opt.beta2 = 1e-1; %penalty parameters
opt.eta1 = 1; opt.eta2 = 1; %lagrangian update
case 'cgs'
opt.linear_solver = 'cgs';
opt.linear_iterations = 10;
opt.inner_iterations = 1;
opt.beta1 = 1; opt.beta2 = 1; %penalty parameters
opt.eta1 = 1.618; opt.eta2 = 1.618; %lagrangian update
case 'l-bfgs'
opt.linear_solver = 'l-bfgs';
opt.linear_iterations = 3;
opt.lbfgs_rank = 5;
opt.inner_iterations = 1;
opt.beta1 = 1; opt.beta2 = 1; %penalty parameters
opt.eta1 = 1.618; opt.eta2 = 1.618; %lagrangian update
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%End parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delta = -1000; %max-margin parameter
funA = @(z) funA_secants_WY(z, secants{i});
funAT = @(z) funAT_secants_WY(z, secants{i});
b = ones(numSecants, 1);
ticID = tic;
[P{i}, L{i}, q{i}, Lambda{i}, w{i}] = NuMax(funA, funAT, b, delta, opt);
toc(ticID);
[U{i}, S{i}, V{i}] = svd(P{i});
r{i} = rank(P{i});
U1{i} = U{i}(:, 1:r{i});
U1{i} = (U1{i} - min(U1{i}(:)))/(max(U1{i}(:))-min(U1{i}(:)));
Phi_NuMax{i} = (U{i}(:, 1:r{i})*(S{i}(1:r{i}, 1:r{i}).^(1/2)))';
Y{i} = Phi_NuMax{i} * data{i};
array1 = Phi_NuMax{i};
whos array1;
array2 = data{i};
whos array2;
theProduct{i} = array1 * array2;
theProduct{i} = reshape(theProduct{i},[],1);
end
K = cell2mat(theProduct);
% make csv file whose name matches the avi file
[~,name] = fileparts(list(k).name); % get the file name without the extension
outfile = strcat(name,'.csv')
csvwrite(outfile,K); % assumes you have already assigned the array X earlier
end

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 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 Commenti
Kong
Kong il 29 Mar 2020
Modificato: Kong il 29 Mar 2020
Thank you so much.
Phi_NuMax{i} is cell.
I want to multiply Phi_NuMax{1} * data{1} and Phi_NuMax{2} * data{2} and so on.
My problem is that the matrix of Phi_NuMax{1} has a different size.
I mean, Phi_NuMax{1,1} = 100 x 144, Phi_NuMax{1,2} = 95 x 144
After multiplying Y{i} = Phi_NuMax{i} * data{i} , I want to flatten Y{i} and concatenate to a matrix.
However, I got an error because of the different sizes.
I want to reduce the size of rows of Phi_NuMax{1,1} = 100 x 144, Phi_NuMax{1,2} = 95 x 144... to Phi_NuMax{1,1} = min X 144.
And then concatenate to a matrix using this code. My problem is this point.
end
K = cell2mat(Y);
Kong
Kong il 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);

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by