Inserting missing rows in a matrix

조회 수: 6 (최근 30일)
Mido
Mido 2016년 11월 2일
답변: KSSV 2016년 11월 2일
If I have a matrix like x = [1 1; 1 2; 2 1; 2 2; 2 3; 3 1; 3 2]
x =
1 1
1 2
2 1
2 2
2 3
3 1
3 2
For every number in the first column I want it to have numbers 1 2 3 in the second column. As it is shown, Number 1 in the first column have 1 and 2 but doesn't have 3. I want to write a code that create a row which will be the third row in this matrix that writes 1 in the first column and 3 in the second column of this matrix. Similarly, I want the code to do the same thing and insert a last row as 3 in the first column have values of 1 and 2 but there is no 3. So, it will create a last row of [3 1].
I appreciate your help.

채택된 답변

KSSV
KSSV 2016년 11월 2일
Why fill gaps? You can make new matrix obeying your conditions.
iwant = zeros(9,2) ;
iwant(:,2) = repmat([1 2 3],1,3);
k = repmat([1 2 3],3,1) ;
iwant(:,1) = k(:);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by