Find and collate matching values.

4 vues (au cours des 30 derniers jours)
Abhi Ove
Abhi Ove le 9 Oct 2017
Commenté : Abhi Ove le 13 Nov 2017
Hi All,
Lets say I have multiple unique data sets:
data1.gamma = [1,2,3,4,5,6,7,8,9,10];
data1.eta = [0.1,0.4,0.9,0,0.2,0.5,0.7,0.8,0.9,0.10];
data1.phi ........
data2.gamma = [0,2,0,4,5,9,7,10,9,6];
data2.eta = [0.1,0.4,0.9,0,0.2,0.5,0.7,0.8,0.9,0.10];
data2.phi ........
and data3, data4 etc as such.
I would like to write a function that can to compare all the data sets and on each data set only keep variable values where
data.gamma
is the same. Any idea how I would go about doing that? I can compare and match 2 data sets. However I am not sure how to compare multiple data sets.
Also I do not know how many data set there will be (data1,data2,data3,data4....). So how do I make the function "scale-up" as necessary. Is that even possible without predefined the variable names?
Any help would be much appreciated.
  5 commentaires
Jos (10584)
Jos (10584) le 12 Oct 2017
Do *NOT* store your data like this. Use an array of structs.
data(1).gamma = ...
data(2).gamma = ...
Jos (10584)
Jos (10584) le 12 Oct 2017
That being said, what is it you want to keep? Can you give a small example of input and required output?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 12 Oct 2017
Not sure what "keep" means, but to compare the two arrays, use isequal(). Try this:
if isequal(data1.gamma, data2.gamma)
% They're equal
else
% They're not equal.
end
It would be best if your datan were not unique as you said, but a structure array. For example if you had 10 versions of data all with different names you'd have 10*9/2 = 45 separate if blocks to compare all possible pairs. If you had one variable, data, that was a structure array, then you don't need to compare all combinations pair-by-pair, but you could do a nested loop to make all comparisons:
for k1 = 1 : length(data)
for k2 = 1 : k1-1
if isequal(data(k1).gamma, data(k2).gamma)
% They're equal
else
% They're not equal.
end
end
end
  1 commentaire
Abhi Ove
Abhi Ove le 13 Nov 2017
Thanks a lot for your answer. I have found your advice very helpful. However I have implemented the following method which seems to work wonderfully.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by