在MATLAB R2011a中,元素的翻转顺序起作用

时间:2015-03-03 16:48:25

标签: matlab matrix flip

我正在使用MATLAB版本R2011a,我的一个朋友正在使用R2014b,其中包含函数" Flip",它翻转了元素的顺序,这个功能对我们的程序比较Matrix' ES。

我的问题是R2011a没有这个功能,它有fliplr,flipud和flipdim。我已经尝试使用fliplr然后翻转尝试重新创建相同的功能,但最终它没有工作,因为我使用的函数corr需要使用它的两个参数是相同的维度。

我需要建议如何创建R2014b上提供的翻转功能。

有问题的功能:

%This function gets the DNA signiture with the relative freq of each perm at
%the refernce text, the DNA signiture with the relative freq of each perm at
%the compare text, and the MaxPerm, and return the relative editor distance  
%between the 2 texts. 

function [distance]=EditorDistance2 (RefDNAWithFreq,CmpDNAWithFreq,MaxPerm)

if MaxPerm>2
   MaxPerm=2; 
end

str='Editor Distance compare begun';
disp(str);

distance=[];

for PermLength=1:MaxPerm

    freq=sum(0:PermLength);
    PermInitial=freq+1;
    permEnd=freq+PermLength;

    %create an ordered matrix of all the perms with length "PermLength"
    %in the ref text
    CurRefPerms=RefDNAWithFreq(:,freq:permEnd); 
    OrderedRefCurPerms=sortrows(CurRefPerms);
    OrderedRefCurPerms=flip(OrderedRefCurPerms);
    OrderedRefCurPerms(:,1)=[];
    OrderedRefCurPerms=ZeroCutter(OrderedRefCurPerms);


    %create an ordered matrix of all the perms with length "PermLength"
    %in the cmp text
    CurcmpPerms=CmpDNAWithFreq(:,freq:permEnd); 
    OrderedCmpCurPerms=sortrows(CurcmpPerms);
    OrderedCmpCurPerms=flip(OrderedCmpCurPerms);
    OrderedCmpCurPerms(:,1)=[];
    OrderedCmpCurPerms=ZeroCutter(OrderedCmpCurPerms);

    len1=size(OrderedRefCurPerms,1);
    len2=size(OrderedCmpCurPerms,1);

    edit=1; 

    matrix=zeros(len2,len1);

    %initiate first row of the first stirng
    for i=2:len1
        matrix(1,i)=matrix(1,i-1)+1;
    end 

    %initiate first column of the second stirng
    for i=2:len2
        matrix(i,1)=matrix(i-1,1)+1;
    end

    %start algoritem
    for i=2:len2
        for j=2:len1


            if OrderedRefCurPerms(j-1,:)==OrderedCmpCurPerms(i-1,:)
                edit=0;
            end


            if (i>2 & j>2 &  OrderedRefCurPerms(j-1,:)==OrderedCmpCurPerms(i-2,:) &  RefDNAWithFreq(j-2)==CmpDNAWithFreq(i-1) )

                matrix(i,j)= min([matrix(i-1,j)+1,...       deletion
                                  matrix(i,j-1)+1,...       insertion
                                  matrix(i-2,j-2)+1,...     substitution
                                  matrix(i-1,j-1)+edit...   transposition
                                  ]);       

            else
                matrix(i,j) = min([matrix(i-1,j)+1,...      deletion
                                   matrix(i,j-1)+1,...      insertion
                                   matrix(i-1,j-1)+edit...  substitution
                                   ]);
            end


            edit=1;
        end

    end

    %The Distance is the last elment of the matrix.
    if i~=1
        tempdistance = matrix( floor( len2 / 3 ) , floor( len1 / 3 ) );
        tempdistance=tempdistance/floor(len2/3);
    else
        tempdistance = matrix( len2,len1 );
        tempdistance= tempdistance/len2;        
    end

    tempdistance=1-tempdistance;

    distance=[distance tempdistance];
end

end

我将进一步解释自己,我尝试使用的功能是A = flip(A)

导致我出现问题的功能就是这个

%此函数获取DNA签名,每个烫发的相对频率为     参考文本%,每个烫发的相对频率的DNA签名     %比较文本和MaxPerm,并返回2个文本之间的相关。

function    [Corvector]=CorrelationCompare(RefDNAWithFreq,CmpDNAWithFreq,MaxPerm)

str='corraltion compare begun';
disp(str);
%this vector will contain the corralation between the freqs of 
%each perms vector(each length)
Corvector=[]; 


for PermLength=1:MaxPerm

    freq=sum(0:PermLength);
    PermInitial=freq+1;
    permEnd=freq+PermLength;


    %Cor is correlation between the 2 texts
    refPerms=RefDNAWithFreq(:,freq);
    cmpPerms=CmpDNAWithFreq(:,freq);

    refPerms=ZeroCutter(refPerms);
    cmpPerms=ZeroCutter(cmpPerms);
    tempCor=corr(refPerms,cmpPerms);
    Corvector =[Corvector tempCor];


%     making a graph of the perms, and the relative freq of the texts.
    x=ZeroCutter ( RefDNAWithFreq(:,PermInitial:permEnd) );
    y1=refPerms;
    y2=cmpPerms;

    xchars=char(x);

    Xcols=size(x,1);
    o=ones(Xcols,1);
    xco=mat2cell(xchars,o,PermLength);
    xaxis=(1:Xcols);

    figure
    stem(xaxis,y1,'r');
    hold
    stem(xaxis,y2,'g');
    set(gca,'XTick',xaxis)
    set(gca,'XTickLabel',xco,'fontname','david');
    xlabel('Perms');
    ylabel('Perm frequency');
    TitleOfGraph=sprintf('comapre between reference text to the compared, %d letters perm\n correlation=%f',PermLength,Corvector(PermLength));
    legend('reference','compared');
    title(TitleOfGraph);

end


end

使用不同的翻转命令时收到的错误是

??? Error using ==> corr at 102
X and Y must have the same number of rows.

Error in ==> CorrelationCompare at 27
tempCor=corr(refPerms,cmpPerms);

我为漫长的代码道歉,但很难解释这一切,因为它是一个大项目,其中很多是由我的合作伙伴完成的

2 个答案:

答案 0 :(得分:2)

这应该适合你 -

function out = flip_hacked(A,dim)

%// Get an array of all possible dimensions
dims = 1:ndims(A);

%// Interchange first dimension and dim
dims(dim) = 1;
dims(1) = dim;
A1 = permute(A,[dims]);

%// Reshape A1 into a 2D matrix and then flip along the first dimension,
%// which would correspond to the flipping along dim and then interchange dim 
%// and first dim again to keep the size of data same as input and elements 
%// being flipped along dim for the desired output
A2 = reshape(A1,size(A1,1),[]);
out = permute(reshape(A2(end:-1:1,:),size(A1)),dims);

return;

它遵循与官方文档中所述的官方 flip 功能相同的语法,如下所示 -

  

B = flip(A,dim)反转A中元素的顺序   维度暗淡。例如,如果A是矩阵,则翻转(A,1)反转   每列中的元素,以及翻转(A,2)反转元素   每一行。

答案 1 :(得分:1)

除了 Divakar 提供的通用解决方案之外,您只需使用:

flip = @(A) A(end:-1:1, :);
A = flip(A);

要反转矩阵A的每列中的元素。更简单:

A = A(end:-1:1, :);