-3

I want to swap half of the elements of an array A with corresponding half of elements of another array B.Is there any built-in function for this operation or is there any shortcut ???Can anyone help me ????

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

Example: input

A={1 2 3 4 5 6};
B={7,8,9,10,11,12}; 

output

C=={1,2,3,10,11,12}
D=={7,8,9,4,5,6}
Dan
  • 45,079
  • 17
  • 88
  • 157
lara
  • 63
  • 7
  • 1
    which half? Just use a temporary variable in between. `temp = A(ind); A(ind) = B(ind); B(ind) = temp;`. `ind` will depend on which elements you want to swap. – Dan Mar 14 '14 at 09:22
  • example:A={1 2 3 4 5 6} , B={7,8,9,10,11,12} I want to exchange 1,2,3 elements of array A with the 7,8,9 of array B output should be 2 new array as given below: C= {1,2,3,10,11,12} D={7,8,9,4,5,6} – lara Mar 14 '14 at 09:44
  • 1
    that's a very simple extension on my above comment, have you tried this your self? I think you should *EDIT* your question to add that example in your comment and also to show some code of what you have tried. This is pretty basic Matlab, you must at least take a shot at it. – Dan Mar 14 '14 at 09:51
  • i just stated coding Matlab.Actually i tried all this.But i forgot to add my own code in question...i apologize for your inconvience – lara Mar 14 '14 at 12:20
  • My code is given below: k=1; for i=1:nwpc for j=i+1:nwpc if(i – lara Mar 14 '14 at 12:22

1 Answers1

5

Well, it's Friday after all...Here's half a dozen ways:

%// 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;
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • code: k=1; for i=1:nwpc for j=i+1:nwpc if(i – lara Mar 14 '14 at 12:16
  • @lara: I made a joke, of course...Point is: MATLAB is interactive. The best way to learn it, is to *interact* with it. You can type `help ` for any command, which shows you how to use that command, and point you to other related commands. You can also use `doc `, which is usually more extended and always contains one or more examples. The take-home message is that, as with any software package, the *documentation* is the place to start, not an internet forum. And in MATLAB, the documentation is *great*. – Rody Oldenhuis Mar 14 '14 at 12:52
  • @lara Stick with method 0.5 here, beginner or not. – Dan Mar 14 '14 at 13:09