Effacer les filtres
Effacer les filtres

How to detect and delete similar value rows in Matlab?

2 vues (au cours des 30 derniers jours)
Jelle Prins
Jelle Prins le 29 Sep 2021
Réponse apportée : Chunru le 29 Sep 2021
Let's say I have an 4x4 matrix with values [1 to 4]:
[1 2 3 4;
1 3 4 2;
1 4 2 4;
1 3 2 2]
As you can see the first column contains 4 of the same values [1]. How can I detect rows/columns containing 4 of the same values and replace them by a value [0]?
Thanks in advance.

Réponses (1)

Chunru
Chunru le 29 Sep 2021
A =[1 2 3 4;
1 3 4 2;
1 4 2 4;
1 3 2 2];
% For columns with the same values
i = find(all(diff(A, 1)==0, 1)); % 1 for 1st dimension; use 2 for 2nd dimension
A(:, i) =0
A = 4×4
0 2 3 4 0 3 4 2 0 4 2 4 0 3 2 2

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by