Matlab 将一半数组元素与另一半数组元素交换

Matlab 将一半数组元素与另一半数组元素交换,matlab,cell-array,Matlab,Cell Array,我想将一个数组A的一半元素与另一个数组B的相应一半元素交换。此操作是否有内置函数或快捷方式???有人能帮我吗 k=1; for i=1:nwpc for j=i+1:nwpc if(i<j) nwP3(k,1:cross_pt)=nwP1(i,1:cross_pt) nwP3(k,cross_pt+1:pc)=nwP1(j,cross_pt+1:pc); k=k+1; nw

我想将一个数组A的一半元素与另一个数组B的相应一半元素交换。此操作是否有内置函数或快捷方式???有人能帮我吗

k=1; 
for i=1:nwpc 
    for j=i+1:nwpc 
        if(i<j) nwP3(k,1:cross_pt)=nwP1(i,1:cross_pt)       
            nwP3(k,cross_pt+1:pc)=nwP1(j,cross_pt+1:pc); 
            k=k+1;
            nwP3(k,1:cross_pt)=nwP1(j,1:cross_pt);    
            nwP3(k,cross_pt+1:pc)=nwP1(i,cross_pt+1:pc); 
            k=k+1; 
         end 
     end
end
输出

C=={1,2,3,10,11,12}
D=={7,8,9,4,5,6}

好吧,毕竟是星期五……这里有六种方法:

%// Method 0: beginning programmer
for i=0:1:2
    c=A(i);
    A(i)=B(i);
    B(i)=c;
end


%// Method 1: MATLAB novice
[A,B] = deal([B(1:3) A(4:end)], [A(1:3) B(4:end)]);


%// Method 1.5: MATLAB novice+        
[A(1:3), B(1:3)] = deal(B(1:3), A(1:3));


%// Method 1.8: ambitious MATLAB novice
function myFunction(A,B)
    %//...
    [A(1:3), B(1:3)] = swap(A(1:3), B(1:3));
    %//...


function [a,b] = swap(b,a)
    % look, no content needed!


%// Method 2: Freshman CS student looking to impress friends
A(1:3) = A(1:3)+B(1:3);
B(1:3) = A(1:3)-B(1:3);   %// Look, no temporary!  
A(1:3) = A(1:3)-B(1:3);   %// Overflow? Pff, that won't ever happen.


%// Method 3: Overambitious CS master forced to use MATLAB  
#include "mex.h"
void mexFunction(int nlhs,       mxArray *plhs[], 
                 int nrhs, const mxArray *prhs[])
{         
    plhs[0]=prhs[1];  // (generated with AutoHotKey)
    plhs[1]=prhs[0];
}


%// Method 4: You and way too many others
1. Go to http://www.stackoverflow.com
2. Ask "the internet" how to do it.
3. Wait until "the internet" has done your work for you.
5. Repeat from step 1 for every new question that pops up.


%// Method 5: GOD HIMSELF!
C = A(1:3);      %// ...will simply use a temporary
A(1:3) = B(1:3);  
B(1:3) = C;

哪一半?只需在两者之间使用一个临时变量<代码>温度=A(ind);A(ind)=B(ind);B(ind)=温度
ind
将取决于您要交换的元素。例如:A={123456},B={7,8,9,10,11,12}我想交换数组A的1,2,3元素和数组B的7,8,9输出应该是2个新数组,如下所示:C={1,2,3,10,11,12}D={7,8,9,4,5,6}这是我上面评论的一个非常简单的扩展,您自己尝试过吗?我认为你应该编辑你的问题,在你的评论中添加这个例子,并展示一些你尝试过的代码。这是一个非常基本的Matlab,你必须至少试一试。我刚刚说明了如何编写Matlab。实际上我已经尝试了所有这些。但是我忘记添加我自己的代码了…我为你的错误道歉。我的代码如下所示:k=1;对于i=1:nwpc对于j=i+1:nwpc如果(i代码:k=1;对于i=1:nwpc对于j=i+1:nwpc如果(i@lara:我开了个玩笑,当然……重点是:MATLAB是交互式的。学习它的最好方法是与之交互。你可以为任何命令键入
help
,它向你展示如何使用该命令,并向你指出其他相关命令。你也可以使用
doc
,它通常扩展得更多,并且总是包含一个或多个命令示例。带回家的信息是,与任何软件包一样,文档是开始的地方,而不是互联网论坛。在MATLAB中,文档很棒。感谢您的支持support@lara在这里坚持使用方法0.5,初学者与否。
%// Method 0: beginning programmer
for i=0:1:2
    c=A(i);
    A(i)=B(i);
    B(i)=c;
end


%// Method 1: MATLAB novice
[A,B] = deal([B(1:3) A(4:end)], [A(1:3) B(4:end)]);


%// Method 1.5: MATLAB novice+        
[A(1:3), B(1:3)] = deal(B(1:3), A(1:3));


%// Method 1.8: ambitious MATLAB novice
function myFunction(A,B)
    %//...
    [A(1:3), B(1:3)] = swap(A(1:3), B(1:3));
    %//...


function [a,b] = swap(b,a)
    % look, no content needed!


%// Method 2: Freshman CS student looking to impress friends
A(1:3) = A(1:3)+B(1:3);
B(1:3) = A(1:3)-B(1:3);   %// Look, no temporary!  
A(1:3) = A(1:3)-B(1:3);   %// Overflow? Pff, that won't ever happen.


%// Method 3: Overambitious CS master forced to use MATLAB  
#include "mex.h"
void mexFunction(int nlhs,       mxArray *plhs[], 
                 int nrhs, const mxArray *prhs[])
{         
    plhs[0]=prhs[1];  // (generated with AutoHotKey)
    plhs[1]=prhs[0];
}


%// Method 4: You and way too many others
1. Go to http://www.stackoverflow.com
2. Ask "the internet" how to do it.
3. Wait until "the internet" has done your work for you.
5. Repeat from step 1 for every new question that pops up.


%// Method 5: GOD HIMSELF!
C = A(1:3);      %// ...will simply use a temporary
A(1:3) = B(1:3);  
B(1:3) = C;