How to find the nearest value of a number by comparing two nested cell arrays?
1 次查看(过去 30 天)
显示 更早的评论
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/252468/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/252469/image.png)
0 个评论
采纳的回答
Adam Danz
2019-12-4
编辑:Adam Danz
2019-12-4
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 个评论
Adam Danz
2019-12-5
编辑:Adam Danz
2019-12-5
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Array Geometries and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!