如何找到满足条件的三维矩阵的N值

时间:2014-08-06 02:34:57

标签: matlab

我有一个由features表示的3D数组。 feature的每个元素都是数字x。现在我将得到该数字并计算数字的g(x)f(x)gf是x的函数)。我的问题是如何在Ng(x)之间获得f(x)绝对值的最大化。该函数将返回一个包含N元素x的数组。但我不知道如何得到它们。你能帮帮我吗?

这是我的代码:

%features is 3D array
%N is elements that we need
%Fs,sigmas,thetas are size of the array
% return N elements of features that maximization abs(f_s-g_s)
function features_filter=gabor_sort(features,N,Fs,sigmas,thetas)

for k = 1:numel(sigmas)
    for j = 1:numel(Fs)
        for i = 1:numel(thetas)
            x= features(:,:,k,j,i);
            f_x=x.^2;
            g_x=x.^3+1;
            s1=abs(f_x-g_x);
            %%Do something in here to get maximization of s1        
         end
    end
end

end

1 个答案:

答案 0 :(得分:2)

这不是问题。创建两个矩阵,用于存储我们为sigma, Fstheta的每个组合获得的要素,并将每个要素的绝对值放在此矩阵中,当您完成后,排序这些距离以降序顺序排列。然后,我们可以使用sort的第二个参数来为我们提供最大化此距离的要素的位置。换句话说,这样做:

%features is 3D array
%N is elements that we need
%Fs,sigmas,thetas are size of the array
% return N elements of features that maximization abs(f_x-g_x)
function features_filter=gabor_sort(features,N,Fs,sigmas,thetas)

s1 = []; % s1 array to store our distances
xFeatures = []; %// Features to return

for k = 1:numel(sigmas)
    for j = 1:numel(Fs)
        for i = 1:numel(thetas)
            x = features(:,:,k,j,i);
            xFeatures = cat(3,xFeatures,x); %// Stack features in a 3D matrix
            x = x(:); %// Convert to 1D as per your comments
            f_x=mean(x.^2); %// Per your comment
            g_x=mean(x.^3+1); %// Per your comment
            s1 = [s1 abs(f_x-g_x)]; %// Add to s1 array
        end
    end
end

[~,sortInd] = sort(s1, 'descend');

%// Return a 3D matrix where each slice is a feature matrix
%// The first slice is the one that maximized abs(f_x - g_x) the most
%// The second slice is the one that maximized abs(f_x - g_x) the second most, etc.
features_filter = xFeatures(:,:,sortInd(1:N));

次要注意:此代码未经测试。我无法访问您的数据,因此我无法真正重现。希望这有效!

相关问题