2015-04-13 59 views
1

我有点基本问题,但我似乎无法解决它。我有两个非常大的数据。我有一个矩阵A是48554 x 1和矩阵B是160272 x 1.我想要做的是“下采样”矩阵B,以便它具有与矩阵A相同的长度。此外,我想要一个函数这可以做到这一点几乎任何大的mx 1和nx 1矩阵(所以它的作用不仅限于这个特定的例子)。我尝试使用resample功能,如下所示:在matlab中下采样或均衡矢量长度

resample(B,length(A),length(B)) 

但是,我收到一个错误,指出过滤器长度过大。接下来,我尝试使用for循环来简单地从矩阵B中提取每个第i个元素,并将其保存到一个新的矩阵中,但由于向量长度不能被整数整除,所以这并不能很好地工作。有没有人有其他(希望简单)的方法来完成这个?

回答

2

您可以尝试使用interp1函数。我知道下采样本身并不是真正的插值。这里有一个例子:

x1 = [0:0.1:pi];%vector length is 32 
x2 = [0:0.01:pi];% vector length is 315 

y1 = sin(x1); 
y2 = sin(x2); 

%down sample y2 to be same length as y1 
y3 = interp1(x2,y2,x1);%y3 length is 32 

figure(1) 
plot(x2,y2); 
hold on 
plot(x1,y1,'k^'); 
plot(x1,y3,'rs'); 

% in general use this method to down-sample (or up-sample) any vector: 
% resampled = interp1(current_grid,current_vector,desired_grid); 
% where the desired_grid is a monotonically increasing vector of the desired length 
% For example: 

x5 = [0.0:0.001:1-0.001]; 
y5 = sin(x); 
% If I want to down sample y5 to be 100 data points instead of 1000: 
y6 = interp1([1:length(y5)],y5,1:1:100); 

% y6 is now 100 data point long. Notice I didn't use any other data except for y6, the length of y6 and my desired length. 
+0

好的,这是否意味着我是正确的说这个问题不能用resample函数解决? –

+0

好吧,我刚刚试过你说的,我得到了这个错误: “网格向量不是单调递增的。” 这种方法是否假定两个向量是相似的函数?他们不在我的情况。我很抱歉,如果我应该指出这一点。 –

+0

好的,对不起,我编辑了我的答案以包含更好的解释。这两个向量是不同的功能并不重要。您看到的错误很可能是因为您为“x”或网格变量输入了错误的输入。再请参阅我的答案以获取更多示例。 – willpower2727