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:
how can I avoid for cycle in this snippet

Subject: how can I avoid for cycle in this snippet

From: Patrick Diviacco

Date: 29 Nov, 2009 04:01:18

Message: 1 of 5

hi,

can I better write the following lines avoid to use a for cycle ?

for i = 1:size(U,2)
   U(:,i) = U(:,i) / norm(U(:,i),2);
end

thanks

Subject: how can I avoid for cycle in this snippet

From: asd def

Date: 29 Nov, 2009 04:24:03

Message: 2 of 5

U(:,1:size(U,2)) = U(:,1:size(U,2)) ./ norm(U(:,1:size(U,2)),2);

?? works

"Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesrme$bbl$1@fred.mathworks.com>...
> hi,
>
> can I better write the following lines avoid to use a for cycle ?
>
> for i = 1:size(U,2)
> U(:,i) = U(:,i) / norm(U(:,i),2);
> end
>
> thanks

Subject: how can I avoid for cycle in this snippet

From: Patrick Diviacco

Date: 29 Nov, 2009 11:03:04

Message: 3 of 5

no, it doesn't work

"asd def" <lovlovlov@indiatimes.com> wrote in message <hest13$2ht$1@fred.mathworks.com>...
> U(:,1:size(U,2)) = U(:,1:size(U,2)) ./ norm(U(:,1:size(U,2)),2);
>
> ?? works
>
> "Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesrme$bbl$1@fred.mathworks.com>...
> > hi,
> >
> > can I better write the following lines avoid to use a for cycle ?
> >
> > for i = 1:size(U,2)
> > U(:,i) = U(:,i) / norm(U(:,i),2);
> > end
> >
> > thanks

Subject: how can I avoid for cycle in this snippet

From: asd def

Date: 29 Nov, 2009 17:53:03

Message: 4 of 5

here you go ...This works....just inspect into how function norm works !!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
U = [1 2 3; 4 5 6; 7 8 9 ; 10 11 12];
U = U./(repmat( (sum(abs(U(:,1:size(U,2))).^2).^(1./2)) , [size(U,1) 1]));
U % Answer
   % 0.077615052570633 0.136717185404933 0.182574185835055
   % 0.310460210282533 0.341792963512332 0.365148371670111
   % 0.543305367994433 0.546868741619731 0.547722557505166
   % 0.776150525706333 0.751944519727130 0.730296743340222
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% SAME AS YOUR FOR LOOP SOLUTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
U = [1 2 3; 4 5 6; 7 8 9 ; 10 11 12];
U = [1 2 3; 4 5 6; 7 8 9 ; 10 11 12];
for i = 1:size(U,2),
    U(:,i) = U(:,i) / norm(U(:,i),2);
end
U % Answer
   % 0.077615052570633 0.136717185404933 0.182574185835055
   % 0.310460210282533 0.341792963512332 0.365148371670111
   % 0.543305367994433 0.546868741619731 0.547722557505166
   % 0.776150525706333 0.751944519727130 0.730296743340222
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Subject: how can I avoid for cycle in this snippet

From: Patrick Diviacco

Date: 29 Nov, 2009 18:07:03

Message: 5 of 5

thanks very much!!

"asd def" <lovlovlov@indiatimes.com> wrote in message <heucdv$9qm$1@fred.mathworks.com>...
> here you go ...This works....just inspect into how function norm works !!
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> U = [1 2 3; 4 5 6; 7 8 9 ; 10 11 12];
> U = U./(repmat( (sum(abs(U(:,1:size(U,2))).^2).^(1./2)) , [size(U,1) 1]));
> U % Answer
> % 0.077615052570633 0.136717185404933 0.182574185835055
> % 0.310460210282533 0.341792963512332 0.365148371670111
> % 0.543305367994433 0.546868741619731 0.547722557505166
> % 0.776150525706333 0.751944519727130 0.730296743340222
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> % SAME AS YOUR FOR LOOP SOLUTION
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> U = [1 2 3; 4 5 6; 7 8 9 ; 10 11 12];
> U = [1 2 3; 4 5 6; 7 8 9 ; 10 11 12];
> for i = 1:size(U,2),
> U(:,i) = U(:,i) / norm(U(:,i),2);
> end
> U % Answer
> % 0.077615052570633 0.136717185404933 0.182574185835055
> % 0.310460210282533 0.341792963512332 0.365148371670111
> % 0.543305367994433 0.546868741619731 0.547722557505166
> % 0.776150525706333 0.751944519727130 0.730296743340222
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Tags for this Thread

No tags are associated with 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