Effacer les filtres
Effacer les filtres

Remove the rows/columns with single values of a matrix

1 vue (au cours des 30 derniers jours)
Dani Tormo
Dani Tormo le 28 Nov 2012
Hello,
I have a matrix quite big (A < 23 x 250000 >). I want to evaluate the first row A(1,:) and eliminate the columns where there are values only repeated once. The number in this first row is an integer and always is increasing.
Lets say:
B = [1 1 3 5 5 5 7 9 9
0.1 0.5 0.2 0.4 0.3 0.9 0.1 0.6 0.5]
The result should be:
B = [1 1 5 5 5 9 9
0.1 0.5 0.4 0.3 0.9 0.6 0.5]
I know how to do it with for and if statements but I think it won't be efficient because the size of the matrix.
Many thanks!

Réponse acceptée

Dani Tormo
Dani Tormo le 28 Nov 2012
This is the code I have and it works, but maybe it is not efficient:
for i = 2:length(A) - 1;
if A(1,i) ~= A(1,i-1) && A(1,i) ~= A(1,i+1)
A = A(:,[1:i-1 i+1:end]);
i = i - 1;
end
end
  4 commentaires
Dani Tormo
Dani Tormo le 3 Déc 2012
Modifié(e) : Dani Tormo le 3 Déc 2012
Yes, finally I had to do it with the while expression to avoid the for fail. The rush isn't good ever.
finished = 0; i = 2;
while finished == 0
if A(1,i) ~= A(1,i-1) && A(1,i) ~= A(1,i+1)
simulation_data = simulation_data(:,[1:i-1 i+1:end]);
elseif i == length(A)-1
finished = 1;
else
i = i + 1;
end
end
Regards.
Jan
Jan le 3 Déc 2012
Modifié(e) : Jan le 3 Déc 2012
Collect the indices to be removed at first:
toDelete = false(size(A, 2));
for i = 2:size(A, 2) - 1
if A(1,i) ~= A(1,i-1) && A(1,i) ~= A(1,i+1)
toDelete(ii) = true;
end
end
A(:, toDelete) = [];

Connectez-vous pour commenter.

Plus de réponses (1)

per isakson
per isakson le 28 Nov 2012
Modifié(e) : per isakson le 28 Nov 2012
A start:
B = [1 1 3 5 5 5 7 9 9
0.1 0.5 0.2 0.4 0.3 0.9 0.1 0.6 0.5];
[ uniqueB, ix ] = unique( B(1,:) );
sB = B(1,:);
sB( ix ) = [];
singleB = setdiff( uniqueB, sB );
[ ~, ixm ] = ismember( singleB, B(1,:) );
B( :, ixm ) = []
prints
B =
1.0000 1.0000 5.0000 5.0000 5.0000 9.0000 9.0000
0.1000 0.5000 0.4000 0.3000 0.9000 0.6000 0.5000
>>
your turn.
I'm wouldn't be surprise if the for-loop is faster :)

Catégories

En savoir plus sur Programming Utilities dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by