Add zero rows to a matrix
Mostrar comentarios más antiguos
Hi, I have the matrix A:
A=[1 5;
2 4;
4 9;
6 3;]
All the elements of the 1st column are integer and arranged ascendingly but there is a jump somewhere (from row 2 to 4 missing 3 and from 4 to 6 missing 5). I want to add zero rows wherever there is jump in the 1st column to become:
A=[1 5;
2 4;
0 0;
4 9;
0 0;
6 3;]
Thanks in advance
Respuesta aceptada
Más respuestas (3)
Image Analyst
el 26 de Ag. de 2016
This well commented code will work:
% Declare sample data. Must be integers!
A=[1 5; 2 4; 4 9; 6 3]
% Get size of A.
[rows, columns] = size(A)
% Extract just the first column.
column1 = A(:, 1)'
% Find out what numbers SHOULD be in the first column.
allNumbers = [A(1,1) : 1 : A(end, 1)]
% Initialize an output array.
A_out = zeros(length(allNumbers), columns)
% Assign existing numbers to the rows where they belong.
A_out(column1,:) = A
1 comentario
Ismaeel
el 26 de Ag. de 2016
James Tursa
el 26 de Ag. de 2016
Assuming all of the numbers in 1st column of A are increasing positive integers, e.g.,
result = zeros(max(A(:,1)),size(A,2));
result(A(:,1),:) = A;
2 comentarios
Azzi Abdelmalek
el 26 de Ag. de 2016
A=[1 5; 2 4; 4 9; 6 3]
A(A(:,1),:)=A
James Tursa
el 26 de Ag. de 2016
Azzi: This does not yield the same result. It puts the current A rows in the proper places but fails to zero out the other rows.
Anton Shagin
el 27 de Nov. de 2017
0 votos
Hi all! Can you help me out with a similar question? I have a 6519x20 matrix filled with data. First 3 columns are month, day and year accordingly. Each day has ~220 data rows. I need to determine missing days and insert missing zero rows into the matrix. I only need to insert one row per missing day as I will rearrange the data and swap matrix rows and columns later on. Therefore each day will have the same amount of data points. I've tried to do it with an example below but it only works for one day per row so it gives me an A-out matrix with 30 rows of data when the goal is to get 6519 + missing rows.
2 comentarios
James Tursa
el 27 de Nov. de 2017
Please open up a new Question for this.
Anton Shagin
el 27 de Nov. de 2017
Editada: Image Analyst
el 27 de Nov. de 2017
James, already did. Thanks!
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!