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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 26일

1 개 추천

A=[1 5; 2 4; 4 9; 6 3]
A(logical(accumarray(A(:,1),1)),:)=A

댓글 수: 4

Ismaeel
Ismaeel 2016년 8월 26일
Oh this is very fast and easy. Smart idea, thank you Azzi.
This does not yield the desired result as stated in the Question. It puts the current A rows in the proper places but fails to zero out the other rows. E.g.,
>> A=[1 5; 2 4; 4 9; 6 3]
A =
1 5
2 4
4 9
6 3
>> A(logical(accumarray(A(:,1),1)),:)=A
A =
1 5
2 4
4 9 <-- this row did not get zeroed out
4 9
0 0
6 3
>> desired_result = [1 5; 2 4; 0 0; 4 9; 0 0; 6 3;]
desired_result =
1 5
2 4
0 0 <-- this row should be all 0's according to Question posted
4 9
0 0
6 3
Either IA's answer or my answer (which are essentially the same) not only put the current A rows in the proper place, but give a result that zeros out the other rows. E.g.,
>> A=[1 5; 2 4; 4 9; 6 3]
A =
1 5
2 4
4 9
6 3
>> result = zeros(max(A(:,1)),size(A,2));
>> result(A(:,1),:) = A
result =
1 5
2 4
0 0 <-- Got the 0's here as desired
4 9
0 0
6 3
I noticed the same thing. Azzi's answer works for the supplied matrix, but not in general. James and my answers both work in the more general case. For example, it will not work in this case
A=[3 5; 4 4; 6 9; 9 3]
while my and James answers will work. My and James answers are essentially the same except that mine has comments and made some intermediate variables to aid in understanding it, while James's code boils it all down to a compact two lines of code.
Yes, this can be corrected by
A=[3 5; 4 4; 6 9; 9 3]
B=[];
B(logical(accumarray(A(:,1),1)),:)=A

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Image Analyst
Image Analyst 2016년 8월 26일

1 개 추천

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

Ismaeel
Ismaeel 2016년 8월 26일
Thank you so much, it works. Appreciate it.

댓글을 달려면 로그인하십시오.

James Tursa
James Tursa 2016년 8월 26일

0 개 추천

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

A=[1 5; 2 4; 4 9; 6 3]
A(A(:,1),:)=A
James Tursa
James Tursa 2016년 8월 26일
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
Anton Shagin 2017년 11월 27일

0 개 추천

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

James Tursa
James Tursa 2017년 11월 27일
Please open up a new Question for this.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2016년 8월 25일

편집:

2017년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by