【问题标题】:Flip order of elements function in MATLAB R2011aMATLAB R2011a 中元素函数的翻转顺序
【发布时间】:2015-05-04 10:18:50
【问题描述】:

我使用的是MATLAB版本R2011a,我的一个朋友正在使用R2014b,它包含“Flip”函数,它可以翻转元素的顺序,这个函数对于我们比较矩阵的程序至关重要。

我的问题是R2011a没有这个功能,它有fliplr、flipud和flipdim。我曾尝试使用 Fliplr,然后使用 Flipud 尝试重新创建相同的函数,但最终它不起作用,因为我使用的是函数 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);

我为冗长的代码道歉,但很难解释这一切,因为这是一个大项目,而且很多都是由我的合作伙伴完成的

【问题讨论】:

  • 您要翻转什么(向量、矩阵...),以及沿哪个维度翻转?更好的是,发布一些重现问题的小示例代码
  • 我正在尝试翻转在两个文本中包含重复排列次数的矩阵,使用翻转的问题是,如果你像这样使用它:B=flip(A),它会复制尺寸。这确保我在 corr 中具有相同的尺寸。
  • 我认为您仍然需要用您刚刚发布的编辑代码解释什么是“有问题的”。如果您希望从2014B 版本中获得flip,而您的2011A 版本中没有,那么请查看发布的解决方案?如果还有更多,你必须更准确地解释它们。
  • 我已经编辑了我的帖子并添加了我的代码和解释

标签: matlab matrix flip


【解决方案1】:

这应该对你有用 -

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 是一个矩阵,则 flip(A,1) 反转 每一列中的元素,并且flip(A,2) 反转中的元素 每一行。

【讨论】:

  • flipdim 可以做到这一点。也许OP想要flip的单输入版本的“沿第一个非单维”行为?目前还不清楚他想要什么
  • @LuisMendo 呵呵!哇,我没看到flipdim!是的,好像是一样的!是的,OP 必须清楚这一点。
  • 我已经编辑了我的帖子并添加了我的代码和解释,请查看
【解决方案2】:

除了 Divakar 提供的通用解决方案之外,您还可以简单地使用:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    相关资源
    最近更新 更多