How to find the nearest value of a number by comparing two nested cell arrays?

8 vues (au cours des 30 derniers jours)
I have a cell array idp{1x58} which contains the indices of local maxima of each time series. I have another cell array qq{1x58} that contains indices of local minima for every time series.
I want to compare every elemt in the array idp with qq and I want to find the closest values above and below the element in idp.
For ex: idp{1,3} = 411.
Comparing it with qq{1,3}, 376 and 497 are the closest values above and below 411.
How do I do this operation?

Réponse acceptée

Adam Danz
Adam Danz le 4 Déc 2019
Modifié(e) : Adam Danz le 4 Déc 2019
pairs is a cell array the same size as idp and qq. Each element is a 1x2 vector of the values in qq that are just below and above the values in idp.
% Inputs
idp = {381 402 411 359};
qq ={[168 262 498 637],[288,564,669],[88 257 376 497 535],[55 159 217 377 525 681]};
% closest values [below, above] idp
pairs = cellfun(@(a,b)[max(b(b<a)),min(b(b>a))],idp,qq,'UniformOutput',false);
If you'd rather return an nx2 matrix,
pairs = cell2mat(pairs');
  6 commentaires
maruljay
maruljay le 5 Déc 2019
Thanks a lot. Based on your suggestions, I wrote the following code and it worked.
for i = 1:length(files)
[~,PM{i}]=islocalmin(TFWS{i,:});
loc{i}=PM{:,i}>0.9;
qq{i}=find(loc{:,i});
pairs = cellfun(@(a,b)[max(b(b<a)),min(b(b>a))],idp,qq,'UniformOutput',false);
if length(pairs{1,i})==1
loc{i}=PM{:,i}>0.1;
qq{i}=find(loc{:,i});
pairs = cellfun(@(a,b)[max(b(b<a)),min(b(b>a))],idp,qq,'UniformOutput',false);
end
end
Adam Danz
Adam Danz le 5 Déc 2019
Modifié(e) : Adam Danz le 5 Déc 2019
Good work!
I just though of a small improvement you could make but it's not necessary. In your current version, there are two lines that call cellfun() and those lines are identical. A small problem with this approach is that if you ever make changes to one line, you need to make the same changes to the other line and that's often overlooked (imagine you make a change a year from now and forget that you must do the same for the other line).
To avoid that problem, you could wrap the cellfun() line into an anonymous function so that it's only defined once. The first line below has been added and the two celfun() lines have been replaced. Also, I added {i} to (idp{i},qq{i}) -- I think that's what you meant to do.
pairsFunc = @(a,b)cellfun(@(a,b)[max(b(b<a)),min(b(b>a))],a,b,'UniformOutput',false);
for i = 1:length(files)
[~,PM{i}]=islocalmin(TFWS{i,:});
loc{i}=PM{:,i}>0.9;
qq{i}=find(loc{:,i});
pairs = pairsFunc(idp{i},qq{i});
if length(pairs{1,i})==1
loc{i}=PM{:,i}>0.1;
qq{i}=find(loc{:,i});
pairs = pairsFunc(idp{i},qq{i});
end
end
Also note that you're overwriting the pairs variable on each iteration instead of storing the values. Is that really what you want to do?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by