计算从一个阵列到另一个阵列的数据

时间:2013-03-28 12:40:49

标签: arrays matlab kernel

我有两个数组,第一个是data_array(50,210),第二个是dest_array(210,210)。目标是使用data_array中的数据计算特定指标的dest_array值,而不使用for-loop。

我是这样做的:

function [ out ] = grid_point( row,col,cg_row,cg_col,data,kernel )
ker_len2 = floor(length(kernel)/2);
op1_vals = data((row - ker_len2:row + ker_len2),(col - ker_len2:col + ker_len2));
out(cg_row,cg_col) = sum(sum(op1_vals.*kernel)); %incorre
end

function [ out ] = sm(dg_X, dg_Y)
%dg_X, dg_Y - arrays of size 210x210, the values - coordinates of data in data_array,
%index of each element - position this data at 210x210 grid

data_array = randi(100,50,210); %data array

kernel = kernel_sinc2d(17,'hamming'); %sinc kernel for calculations
ker_len2 = floor(length(kernel)/2);

%adding the padding for array, to avoid 
%the errors related to boundaries of data_array
data_array = vertcat(data_array(linspace(ker_len2+1,2,ker_len2),:),...
                     data_array,...
                     data_array(linspace(size(data_array,1)-1,size(data_array,1) - ker_len2,ker_len2),:));

data_array = horzcat(data_array(:,linspace(ker_len2+1,2,ker_len2)),...
                     data_array,...
                     data_array(:,linspace(size(data_array,2)-1,(size(data_array,2) - ker_len2,ker_len2)));

%cg_X, cg_Y - arrays of indicies for X and Y directions
[cg_X,cg_Y] = meshgrid(linspace(1,210,210),linspace(1,210,210));

%for each point at grid(210x210) formed by cg_X and cg_Y, 
%we should calculate the value, using the data from data_array(210,210).

%after padding, data_array will have size (50 + ker_len2*2, 210 + ker_len2*2)

dest_array = arrayfun(@(y,x,cy,cx) grid_point(y, x, cy, cx, data_array, kernel),...
                      dg_Y, dg_X, cg_Y, cg_X);
end

但是,似乎arrayfun无法解决我的问题,因为我使用不同大小的数组。有人有这个想法吗?

1 个答案:

答案 0 :(得分:1)

我不完全确定,但从标题来看,这可能是你想要的:

%Your data
data_array_small = rand(50,210)
data_array_large = zeros(210,210)
%Indicating the points of interest
idx = randperm(size(data_array_large,1));
idx = idx(1:size(data_array_small,1))

%Now actually use the information:
data_array_large(idx,:)  = data_array_small