Matlab中的逻辑索引 [英] logical indexing in matlab
问题描述
请考虑以下示例:
time = [733774,733774,733775,733775,733775,733776,733776];
depth = [0,10,0,5,10,0,10];
d = [1,1.3,1,2.5,2.5,1,1.2];
data = horzcat(time',depth',d');
dz = 1;
在这里,我分别进行了三天的多次测量.第一天在2个不同的深度进行2次测量,第二天在3个深度进行3次测量,第三天在2个深度进行2次测量.
Here I have a number of measurements taken for three separate days. The first day has 2 measurements taken at 2 separate depths, the second day has 3 measurements taken at 3 depths and the third has 2 measurements taken at 2 depths.
我想生成一个新变量'newData',它对数据中的值进行线性插值(interp1).但是,我只想在任何给定日期的测量次数超过2的情况下执行此操作.因此,对于上面的示例,这仅适用于733775.在这里,我想进行深度测量并增加垂直分辨率,例如
I would like to generate a new variable 'newData' which linearly interpolated (interp1) the values in data. However, I would only like to do this if the number of measurements for any given day exceeds 2. So, for the example above this would only apply to 733775. Here I would like to take the depth measurements and increase the vertical resolution e.g.
newDepth = min(depth):dz:max(depth);
但是只能在测量次数超过2的日子中执行此操作.因此,我所描述的结果应该是:
But only perform this for the days when the number of measurements exceed 2. The outcome of what I have described should therefore be:
733774 0
733774 10
733775 0
733775 1
733775 2
733775 3
733775 4
733775 5
733775 6
733775 7
733775 8
733775 9
733775 10
733776 0
733776 10
加上'data(:,3)'中的插值.
plus the interpolated values from 'data(:,3)'.
实现此目标的最佳方法是什么?
What would be the best way of achieving this?
推荐答案
这是执行此操作的一种方法(这次解决了OP的编辑版本):
Here's one way to do this (solving the edited version of the OP this time):
[idx,~,uniqueTimes] = grp2idx(time);
counts = hist(time,uniqueTimes);
%# expand data with counts greater than minCount
minCount = 2;
dz = 1;
expandedData = mat2cell(data,counts,3)
for ii = find(counts>minCount)'
minZ = expandedData{ii}(1,2);
maxZ = expandedData{ii}(end,2);
intData(:,2) = (minZ:dz:maxZ)';
intData(:,1) = expandedData{ii}(1,1);
intData(:,3) = interp1(expandedData{ii}(:,2),expandedData{ii}(:,3),intData(:,2));
expandedData{ii} = intData;
end
out = cat(1,expandedData{:})
这篇关于Matlab中的逻辑索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!