Effacer les filtres
Effacer les filtres

how to delete all row contain a 1's and how to delete a column contain 1's. Q=[3 1 1 0 0 1;1 3 1 0 1 0;1 1 3 1 0 0;0 0 1 3 1 1;0 1 0 1 3 1;1 0 0 1 1 3]

1 vue (au cours des 30 derniers jours)
Q=[3 1 1 0 0 1;1 3 1 0 1 0;1 1 3 1 0 0;0 0 1 3 1 1;0 1 0 1 3 1;1 0 0 1 1 3]
i want to delete rows that contain a 1's and also column that contain 1's.
as in this example the first row contain 1's in 2,3,6 column ..so deleted it. now the first column contain a 1's in 2,3,6. so deleted this rows.now the resulted matrix should be [3 1: 1 3]. i want to check it for any n*n matrix.
thanku.

Réponse acceptée

Jan
Jan le 22 Juin 2022
Following your instructions: "Actually firstly i want to deleted the all columns that contain a 1's in first row. Then we check in the first column that contain a 1's then we deleted the rows" I get another result:
Q = [3 1 1 0 0 1;1 3 1 0 1 0;1 1 3 1 0 0;0 0 1 3 1 1;0 1 0 1 3 1;1 0 0 1 1 3]
Q = 6×6
3 1 1 0 0 1 1 3 1 0 1 0 1 1 3 1 0 0 0 0 1 3 1 1 0 1 0 1 3 1 1 0 0 1 1 3
Q(Q(:, 1) == 1, :) = [];
Q(:, Q(1, :) == 1) = [];
Q
Q = 3×3
3 0 0 0 3 1 0 1 3

Plus de réponses (2)

Jon
Jon le 22 Juin 2022
Modifié(e) : Jon le 22 Juin 2022
icd = any(Q==1,1)
ird = any(Q==1,2)
Qnew = Q(~ird,~icd)
In the example matrix you provide, every row and every column contain a 1 so the result will be an empty matrix
  4 commentaires
Jon
Jon le 22 Juin 2022
If I follow your instructions I get the same answer as @Jan, that is
Q = 3×3
3 0 0
0 3 1
0 1 3
In your picture, it looks like you also delete the first row and the first column, is this done as a final step? If so, in this final step, do you just delete the first column or all of the columns up to an including the first column that contains a 1?
In this case you could do it like this:
Q = [3 1 1 0 0 1
1 3 1 0 1 0
1 1 3 1 0 0
0 0 1 3 1 1
0 1 0 1 3 1
1 0 0 1 1 3]
% delete columns where entry in first row is a 1
Q(:,Q(1,:)==1) = [];
% find first column that has a 1
idx = find(any(Q==1),1,'first');
% delete all the rows where 1 appears in the first column that has a 1
Q(Q(:,idx)==1,:)=[]
% delete the first row
Q(1,:) = []
% delete all of the columns up to the first one that had a 1
Q(:,1:idx)=[]
Kajal Agrawal
Kajal Agrawal le 23 Juin 2022
ok..thanks a lot for answering and solving my problem.

Connectez-vous pour commenter.


Karim
Karim le 22 Juin 2022
Modifié(e) : Karim le 22 Juin 2022
you can use some logic to find them:
% using example data
Q = [3 1 1 0 0 1;1 3 1 0 1 0;1 1 3 1 0 0;0 0 1 3 1 1;0 1 0 1 3 1;1 0 0 1 1 3]
Q = 6×6
3 1 1 0 0 1 1 3 1 0 1 0 1 1 3 1 0 0 0 0 1 3 1 1 0 1 0 1 3 1 1 0 0 1 1 3
Val = 1;
% find the rows and columns that contain the value "1"
RowIdx = any(Q==Val, 2);
ColIdx = any(Q==Val, 1)';
Q = Q(~RowIdx,~ColIdx); % keep the rows and columns of intrest
% display result
Q
Q = []
as you can see, using your example data all rows and colums are deleted... hence below with some random data
Q = randi([0 15],10,10)
Q = 10×10
2 13 2 12 0 10 5 13 9 7 8 4 10 12 13 1 12 0 4 7 12 4 7 8 10 13 2 5 4 13 0 6 15 8 10 7 5 12 0 0 0 0 9 7 7 7 6 5 1 8 8 5 7 4 15 3 13 12 0 2 1 3 9 13 10 1 0 10 2 12 14 15 15 9 9 15 2 1 5 0 6 9 10 8 2 6 2 9 0 4 12 14 13 14 1 8 14 9 4 4
Val = 1;
% find the rows and columns that contain the value "1"
RowIdx = any(Q==Val, 2);
ColIdx = any(Q==Val, 1)';
Q = Q(~RowIdx,~ColIdx); % keep the rows and columns of intrest
% display result
Q
Q = 5×5
13 2 12 5 7 4 7 8 2 13 6 15 8 5 0 5 7 4 13 2 9 10 8 2 4
  3 commentaires
Karim
Karim le 22 Juin 2022
Indeed, sorry i didn't notice that you already answerd it
Jon
Jon le 23 Juin 2022
Modifié(e) : Jon le 23 Juin 2022
It happens to me frequently, that is I submit something only to find that there is already a similar answer. Good in a way, it shows that there is some consensus on the approach

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by