Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Column Operations Matlab

Subject: Column Operations Matlab

From: Donatello

Date: 26 Jun, 2010 03:14:04

Message: 1 of 3

plz i need the script to change the columns of a matrix
example
1 2 3 3 2 1
4 5 6 ---------------> 6 5 4
7 8 9 9 8 7
this is my code....

function invier=cambifil(A)
[f,c]=size(A);
for i=1:f;
    for j=1:c;
       A(i,c-j+1)=A(i,j);
        
    end
end
invier=A
 

Subject: Column Operations Matlab

From: Matt Fig

Date: 26 Jun, 2010 03:24:04

Message: 2 of 3

A(:,size(A,2):-1:1)

or (somewhat slower)

A(:,end:-1:1)

or, equivalently (and perhaps easier on the eyes)

fliplr(A)

Subject: Column Operations Matlab

From: Roger Stafford

Date: 26 Jun, 2010 04:24:04

Message: 3 of 3

"Donatello " <Manguaco7@hotmail.com> wrote in message <i03r9s$47$1@fred.mathworks.com>...
> plz i need the script to change the columns of a matrix
> example
> 1 2 3 3 2 1
> 4 5 6 ---------------> 6 5 4
> 7 8 9 9 8 7
> this is my code....
>
> function invier=cambifil(A)
> [f,c]=size(A);
> for i=1:f;
> for j=1:c;
> A(i,c-j+1)=A(i,j);
> end
> end
> invier=A
- - - - - - - - - -
  Matt's code will work but I'm afraid your code will not, Donatello. The trouble is that when you do A(i,c-j+1)=A(i,j) for i = 1, j = 1, and c = 3, the value 3 that currently resides in A(1,3) will be overwritten by a 1. Then subsequently when i = 1, j = 3, the value at A(1,3) which was previously a 3 is now the 1 that was written into it. Hence the 3 value is lost forever and you will not be able to place it into A(1,1).

  Matt's code is superior, but it would be instructive for you to see how your for-loop method could be made to work.

 c2 = floor(c/2);
 for i = 1:f
  for j = 1:c2
   t = A(i,j); % Temporary storage
   A(i,j) = A(i,c-j+1);
   A(i,c-j+1) = t; % The swap is complete
  end
 end

This way you interchange the two values to be swapped before either is destroyed by providing a temporary storage for one of them.

  It is an easy kind of mistake for beginners to make in rearranging or modifying the contents of matrices. They sometimes forget that in for-loops these changes do not all occur simultaneously but rather one at a time. With vectorized techniques as for example Matt's, changes are automatically stored in special temporary buffers until they are complete and then the result is written out, which avoids the overwriting trouble. With your for-loops you could also accomplish the same thing by writing the reversed data into a temporary matrix T leaving A untouched and then finally finishing with

 A = T;

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us