Effacer les filtres
Effacer les filtres

How declare for the matlab matrix that an value don't is considerable ??

2 vues (au cours des 30 derniers jours)
Carlos Batista
Carlos Batista le 11 Juil 2014
Commenté : dpb le 13 Juil 2014
How declare for the matlab matrix that an value don't is considerable?
I'm opening netcdf data and I want that the matlab ignore a specific value.
How do I?
Thanks! Carlos
  1 commentaire
Carlos Batista
Carlos Batista le 11 Juil 2014
I have a exemplo:
my matriz
a=[1,2,3,4,5]
I want ignore the value 2 for my matrix have only: (1,3,4,5) without the value "2"
How I do ?

Connectez-vous pour commenter.

Réponse acceptée

Titus Edelhofer
Titus Edelhofer le 11 Juil 2014
You can remove elements from your matrix as follows:
a = [1 2 3 4 5];
% delete all two's:
a(a==2) = [];
Titus
  2 commentaires
Carlos Batista
Carlos Batista le 11 Juil 2014
thanks Titus!!!! It's ok, now!!!
Now I need to do the same thing for an larger matrix!!!
I will go test!
Thanks!
Carlos Batista
Carlos Batista le 11 Juil 2014
Titus
It's ok!!!
Well, now I want to ignore the 2 without erasing my matrix. How do I?

Connectez-vous pour commenter.

Plus de réponses (3)

dpb
dpb le 11 Juil 2014
a=a(a~=2);
or
a(a==2)=[];
will replace a. If need to keep a for future reference, use the first form but assign RHS to another variable. NB: if the values aren't integers, floating point comparisons for exact values are not always reliable, you'll need to use "fuzzy" comparison or otherwise account for it.
  2 commentaires
Carlos Batista
Carlos Batista le 11 Juil 2014
thanks dpb!!! It's ok, now!!!
Now I need to do the same thing for an larger matrix!!!
I will go test!
Thanks!
Carlos Batista
Carlos Batista le 11 Juil 2014
dpb
Now I want to ignore the 2 without erasing my matrix. How do I?
thanks!

Connectez-vous pour commenter.


dpb
dpb le 11 Juil 2014
The size of the array has no bearing on the logic.
What I said, save it to another...
b=a(a~=2);
Or, there's the alternative of just using "logical addressing" directly without actually making a copy.
res_notwos=mean(a(a~=2)); % mean excluding elements equal 2
Or another alternative is
a(a==2)=nan; % nan as placeholder
res=nanmean(a); % Stat Toolbox has several such "NaN-aware" functions
The latter is particularly convenient when plotting as plot and friends simply ignore NaNs as well.
All depends on what you're objectives are as to which is the more convenient construct.
  7 commentaires
Carlos Batista
Carlos Batista le 13 Juil 2014
How to make any math operation when there are NaN in data serial?
I'd like to make an test using in my data serial NaN...
For exemplo: I have 2 matrix (a and b) and I'd like of sum them!!!
Ex:
a=[1,2,3,NaN,5]
b=[1,2,3,8,5]
c=(a+b)
How can I to do?
dpb
dpb le 13 Juil 2014
Again, depends on what you mean by "sum" for the case...if you mean "ignore locations where value is NaN" then as noted before, if you have the Statistics Toolbox there are specialized funtions --
>> a=[1,2,3,NaN,5]; b=[1,2,3,8,5];
>> c=nansum([a;b])
c =
2 4 6 8 10
If you don't have the Toolbox, you've got to write special code to do the same thing. Sometimes you can be "tricky" -- for addition,
>> a(isnan(a))=0;
>> c=a+b
c =
2 4 6 8 10
>>
For multiplication, the magic value is '1'. In general you must basically process on an element by element basis taking action as desired when find the NaN. Often an anonymous function and arrayfun and/or accumarray can help immeasurably.
But, there is no one general answer.

Connectez-vous pour commenter.


haseeb
haseeb le 11 Juil 2014
a(:,2)=[];
try this

Community Treasure Hunt

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

Start Hunting!

Translated by