How to add 0 in a column to form an array in which all columns are with equal rows?
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matheus Brito
el 10 de Jul. de 2019
Respondida: Kevin Phung
el 10 de Jul. de 2019
I have 8 txt files, containing 2 columns each file. However each column has a different number of rows.
This way, how can I join these 16 columns into just one file and insert 0 at the end of each column so that they all have the same row size as my largest column???.
thank you!
0 comentarios
Respuesta aceptada
Kevin Phung
el 10 de Jul. de 2019
here is an example:
a = [1 2 3]';
b = [1 2]';
c = [1 2 3 4 5 6 7]';
sets = {a,b,c}; % let a, b, and c be your column sets. Store them in an array 'sets'
max_size = 0;
%figure out the maximum size
for i = 1:numel(sets)
if size(sets{i},1) > max_size
max_size = size(sets{i},1); % set max size, if current column is greater than the current max size
end
end
% append zeros if smaller than the max size
for i = 1:numel(sets)
if size(sets{i},1) < max_size
sets{i} = vertcat(sets{i},zeros(max_size-size(sets{i},1),1));
end
end
sets = cell2mat(sets) %concatenate into a matrix
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!