Compare and compile matching values from various data set.

3 vues (au cours des 30 derniers jours)
Abhi Ove
Abhi Ove le 11 Oct 2017
Commenté : Abhi Ove le 13 Nov 2017
Hi All, I have a problem where I need to compare various data set and only keep the values where the same matching values exist. Let me explain in a bit more detail.
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 for the values of
data.gamma
I would like to keep/store the values where the same values of data.gamma values exists in all data set(data1, data2, data3, data4...). I would also like to mention that the data sets (data1,data2,data3) might not be the same size.
I know and can compare and match two data sets. However I am not sure how to compare multiple data sets. Any idea how I would go about doing that?
Any help would be much appreciated.

Réponse acceptée

OCDER
OCDER le 11 Oct 2017
Modifié(e) : OCDER le 11 Oct 2017
First, do not label variables dynmically like data1, data2, data3, etc.. as this will make your function very complicated. In your situation, using a nonscalar structure would be better such as data(1).gamma, data(2).gamma, data(3).gamma, etc.
Once you restructure your data, you could use intersect to determine common numbers:
data(1).gamma = [1,2,3,4,5,6,7,8,9,10];
data(1).eta = [0.1,0.4,0.9,0,0.2,0.5,0.7,0.8,0.9,0.10];
data(2).gamma = [0,2,0,4,5,9,7,10,9,6];
data(2).eta = [0.1,0.4,0.9,0,0.2,0.5,0.7,0.8,0.9,0.10];
data(3).gamma = [2,4,5,7,10];
data(3).eta = [0.1,0.4,0.9,0,0.2,0.5,0.7,0.8,0.9,0.10];
SameGamma = data(k).gamma;
for k = 2:length(data)
SameGamma = intersect(SameGamma, data(k).gamma);
end
SameGamma =
2 4 5 7 10
  3 commentaires
OCDER
OCDER le 12 Oct 2017
Hi Abhi, here's how you would do it if you use structured data:
SameGamma = data(k).gamma;
for k = 2:length(data)
SameGamma = intersect(SameGamma, data(k).gamma);
end
datafields = fieldnames(data);
for k = 1:length(data)
[~, Idx, ~] = intersect(data(k).gamma, SameGamma);
for f = 1:length(datafields)
data(k).(datafields{f}) = data(k).(datafields{f})(Idx);
end
end
If you want to uses data1, data2, data3,... data10, then you have to type it all out 10 times instead of using the for loop. OR, you need to use save/load workarounds:
save('temp.mat', '-regexp', 'data')
T = load('temp.mat')
* Do same calc as above, but:
replace : data(k).gamma
with this: T.(['data' num2str(k)]).gamma
save('temp2.mat', 'T', '-struct');
load('temp2.mat') %Poof variables back out
Either way, it'll be cumbersome to work with dynamically named variables... Hope this helps.
Abhi Ove
Abhi Ove le 13 Nov 2017
Many thanks. I have taken your advice and made the whole process automated.
I understand the concern with individual data set that required somewhat human input during analysis. This was because I had to work with a set of data as I had received them. I found it easier to restructure the whole results data set using a separate script for better usability. Now everything flows a lot better.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical 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