Assign a certain element value if two array elements are equal
Afficher commentaires plus anciens
Hello. I have two arrays called data1 and data1arranged:
data1 =
100.5000
107.4000
.
.
107.4000
105.6000
data1 is 95x1 double and data1arranged is a 263x1 double. I want to compare every value of data1 to data1arranged and assign what element rank/number it is. Here's my code:
for i=1:numel(data1)
for j = 1:numel(data1arranged)
if ((data1(i,1))==(data1arranged(j,1)))
obsseq(i,1) = observation(j,1);
end
end
end
my data1arranged is just basically an array of numbers from min(data1) to max(data1) with an increment of 0.1. That is, if my min(data1) is 94.2 and if my max(data1) is 120.4, then my data1arranged will just be
data1arranged =
94.2
94.3
94.4
.
.
.
120.3
120.4
observation array is just basically a numbering sequence of data1arranged. That is, from lowest to highest, I would just number it from 1 up to 263. That is basically the equivalent rank of every element in data1arranged.
Observation array is:
for i = 1:numel(data1arranged)
observation(i,1) = i
end
But then every time, I will run the above mentioned for loop, my obsseq will give me the logical value of
(data1(i,1))==(data1arranged(j,1))
of which I'm not interested in. I want that, if the logical value is true, then the obsseq array will fill up with the corresponding value from the observation array elements. Can anyone please help me? Thank you so much!
Réponses (1)
dpb
le 25 Fév 2017
doc ismember
NB: Be aware ismember is exact equality and floating point rounding can wreak havoc on comparisons if values between the two arrays aren't computed similarly. Later versions have ismembertol to handle this.
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!