From: Donatello on 25 Jun 2010 23:19 i need create a function to chage the columns 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)=B(i,j); B(i,j)=A(i,j); end end invier=A
From: Matt Fig on 25 Jun 2010 23:24 A(:,size(A,2):-1:1) or (somewhat slower) A(:,end:-1:1) or, equivalently (and perhaps easier on the eyes) fliplr(A)
From: Roger Stafford on 26 Jun 2010 00:24 "Donatello " <Manguaco7(a)hotmail.com> wrote in message <i03r9s$47$1(a)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
From: Faraz Afzal on 26 Jun 2010 11:28 Dear Donatello, Please try always to explain your problem clearly. Anyways Here is your code.. function invier=cambifil(A) [r,c]=size(A); B = zeros(size(A)); for n=1:c B(:,n) = A(:,c-n+1); end B for example you can use this matrix, (Making your life more easier) A = [ 1 11 21 2 12 22 3 13 23 4 14 24 5 15 25 6 16 26 7 17 27 8 18 28 9 19 29] Regards, Muhammad Faraz "Donatello " <Manguaco7(a)hotmail.com> wrote in message <i03rj8$i3p$1(a)fred.mathworks.com>... > i need create a function to chage the columns > 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)=B(i,j); > B(i,j)=A(i,j); > > end > end > invier=A >
From: us on 26 Jun 2010 13:17 "Donatello " <Manguaco7(a)hotmail.com> wrote in message <i03rj8$i3p$1(a)fred.mathworks.com>... > i need create a function to chage the columns > 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)=B(i,j); > B(i,j)=A(i,j); > > end > end > invier=A > a hint: help fliplr; help flipdim; us
|
Pages: 1 Prev: Column Operations Matlab Next: How to obtain the parallized cpu time? |