Inserting missing rows in a matrix

2 次查看(过去 30 天)
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 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by