Effacer les filtres
Effacer les filtres

replace values in columns of a matrix

1 vue (au cours des 30 derniers jours)
Christopher
Christopher le 22 Nov 2014
Commenté : Christopher le 22 Nov 2014
I have a matrix of random numbers, with some repeating. For example:
OV =
29.5 -29.3 82.2 -49.1
12.6 -62.2 82.2 -72.8
114.8 -29.3 -6.9 156
29.5 -97.1 -42.5 71.1
-63.8 119 33.3 -49.1
I want to do some columnwise operations based on the values of the first row. So far I have
idxo = bsxfun(@ne,OV,OV(1,:))*2.5; % =0 for OV(1,:)=OV and 2.5 otherwise
idxo(sec_ovec<0) = 1; % for OV<0, =1
which gives me a new matrix idxo as follows:
0 1 0 1
2.5 1 0 1
2.5 1 1 2.5
0 1 1 2.5
1 2.5 2.5 1
The last thing I need is to, for all OV rows which have a negative number in the first row: make all corresponding idxo values =0 if OV is negative, and =1 otherwise.
I can do this, but my solution is inefficient. What is a fast way to do this? Speed is important because it is a big bottleneck in my code.
The end result should modify column 2 and 4 to give
idxo =
0 0 0 0
2.5 0 0 0
2.5 0 1 1
0 0 1 1
1 1 2.5 0

Réponse acceptée

Matt J
Matt J le 22 Nov 2014
Modifié(e) : Matt J le 22 Nov 2014
j=OV(1,:)<0;
idxo(:,j)=OV(:,j)>=0;
  1 commentaire
Christopher
Christopher le 22 Nov 2014
Excellent solution, thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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