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:
matlab loop

Subject: matlab loop

From: k.vigneshwaran K

Date: 16 Apr, 2013 04:06:08

Message: 1 of 7

Respected sir,
   I have a problem with for loop, i explained here as below.

for i=1:1:n
LHS(i)=RHS(i)
end

for each and every value of i , I need the individual LHS and RHS.How can i retrieve it from this loop,OR Please guide me the loop to how to do that problem.

THANKING YOU SIR.

Subject: matlab loop

From: Barry Williams

Date: 16 Apr, 2013 10:14:07

Message: 2 of 7

"k.vigneshwaran K" wrote in message <kkiinf$hda$1@newscl01ah.mathworks.com>...
> Respected sir,
> I have a problem with for loop, i explained here as below.
>
> for i=1:1:n
> LHS(i)=RHS(i)
> end
>
> for each and every value of i , I need the individual LHS and RHS.How can i retrieve it from this loop,OR Please guide me the loop to how to do that problem.
>
> THANKING YOU SIR.

The most obvious answer is that with no semicolon at the end of the second line of your code, the individual values will display in the command window as they are generated:
LHS(i) = RHS(i); % The semicolon here suppresses output to the command window.

On the other hand, what is the purpose of your loop? Is there code between the first two lines that is not shown that determines the values of RHS? Otherwise you could merely be reassigning LHS = RHS(1:n)

If the code is embedded in a function, a better way of recovering the values in the two arrays is to make them outputs of your function. Note, by the way, that for loops in ML increment by 1 by default:
for i = 1:n; % Equivalent to for i = 1:1:n

Barry

Subject: matlab loop

From: k.vigneshwaran K

Date: 16 Apr, 2013 12:24:08

Message: 3 of 7

Dear sir,
l(i) value will change with respect to n no of times.

l(i-1)=0;
l(i) =l; %THE INCREMENT OF l(i) is l
l(i+1)=l+l;

due to the every value of l(i),i want to retrieve the KR VALUES.
example.
if l(1)=l; then my
KR=SOMETHING xx for i=1,
at the same time i shall retrieve the forthcoming values of KR.

here i is changing for every elements of i,and also due to the different value of L I should get KR VALUE.

THE LOOP WHICH I MADE IT SHOWN TO YOU FOR YOUR REFERENCE SIR.

K=4 CROSS 4 MATRIX
l(i)=l
    for i=1:2
        KR=l(i)*K;
        l(i)=l(i)+l;
     end
for this loop matlab showing like below

??? Subscript indices must either be real positive integers or
logicals.


PLEASE GUIDE ME TO OBTAIN THE SOLUTON.

THANKING YOU SIR.
 
   

Subject: matlab loop

From: Steven_Lord

Date: 16 Apr, 2013 13:54:29

Message: 4 of 7



"k.vigneshwaran K" <vignesh_tooling@yahoo.co.in> wrote in message
news:kkjft8$4a0$1@newscl01ah.mathworks.com...
> Dear sir,
> l(i) value will change with respect to n no of times.
>
> l(i-1)=0; l(i) =l; %THE INCREMENT OF l(i) is l
> l(i+1)=l+l;
>
> due to the every value of l(i),i want to retrieve the KR VALUES.
> example. if l(1)=l; then my KR=SOMETHING xx for i=1,
> at the same time i shall retrieve the forthcoming values of KR.
>
> here i is changing for every elements of i,and also due to the different
> value of L I should get KR VALUE.
>
> THE LOOP WHICH I MADE IT SHOWN TO YOU FOR YOUR REFERENCE SIR.
>
> K=4 CROSS 4 MATRIX
> l(i)=l

There's no such thing as the sqrt(-1)st element of a matrix in MATLAB.
Unless you'd previously assigned a value to the variable i that's what
you're trying to do with that line of code.

*snip*

I don't understand the bigger picture of what you're trying to do. Describe
IN WORDS NOT IN CODE what specifically you're trying to do and someone may
be able to offer suggestions for how to achieve your goal.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: matlab loop

From: dpb

Date: 16 Apr, 2013 14:17:53

Message: 5 of 7

On 4/16/2013 7:24 AM, k.vigneshwaran K wrote:
> Dear sir,
> l(i) value will change with respect to n no of times.
>
> l(i-1)=0; l(i) =l; %THE INCREMENT OF l(i) is l
> l(i+1)=l+l;
>
> due to the every value of l(i),i want to retrieve the KR VALUES.
> example. if l(1)=l; then my KR=SOMETHING xx for i=1,
> at the same time i shall retrieve the forthcoming values of KR.
>
> here i is changing for every elements of i,and also due to the different
> value of L I should get KR VALUE.
>
> THE LOOP WHICH I MADE IT SHOWN TO YOU FOR YOUR REFERENCE SIR.
>
> K=4 CROSS 4 MATRIX
> l(i)=l
> for i=1:2
     KR=l(i)*K;
> l(i)=l(i)+l;
> end
> for this loop matlab showing like below
>
> ??? Subscript indices must either be real positive integers or
> logicals.
>
>
> PLEASE GUIDE ME TO OBTAIN THE SOLUTON.

Your cap-lock key seems to get stuck on...check your keyboard.

A) 'i' and 'j' are builtin functions --> sqrt(-1) in Matlab so the first
line above is trying to reference and array by that value as an index.
Obviously this isn't kosher so that's the error message.

B) Matlab array indexing is 1-based, not 0-based so your first index
_MUST_ always be >0 (or >=1))(*).

Using the crystal ball, perhaps you meant something like

l(1)=l; % initialize a first location before starting a loop
for i=1:2
   KR=l(i)*K;
   l(i)=l(i)+l;
end

The above would have the result of KR successively becoming n*K where n
= 1, 2. The end result unless you do something else with it inside the
loop before the next iteration will be only KR=N*K where in you example
N=2 since you overwrite KR every pass.

The above result could be obtain simply by using the loop index instead
of the auxiliary variable 'l' as

for i=1:2
   KR=i*K;
end

with the same end result, of course.

As Steven says, explain the end result w/o trying to write the code so
folks can try to get the underlying objective instead of trying to
decipher incorrect coding. Understanding that there's a language
barrier for most readers of cs-sm, bear with us as we try to interpret
but also work from your end to provide a precise definition of the
problem in a clear form not (or at least minimally) confounded by
programming issues.

(*) Ignoring for the moment the logical index which means something
quite entirely different than the absolute value of 0,1 but is positional.

--

Subject: matlab loop

From: k.vigneshwaran K

Date: 17 Apr, 2013 04:09:08

Message: 6 of 7

Respected sir,

your code is perfectly correct for getting KR Value with loop.

but for each and every i value i ca get the KR value.that KR Values only i need in a specified name like that.please guide me to obtain sir.

for i=1:2
   KR=i*K;
end

THE Above K is a 4 x 4 Matrix.
HOW CAN I GET THE RESULTS LIKE FOLLOWING.

 if i=1
  my KR1=K
if i=2
  my KR2=2K
       .
       .
       .
if i=n
   my KRn=nK

THANKING YOU SIR.

Subject: matlab loop

From: Steven_Lord

Date: 17 Apr, 2013 13:44:58

Message: 7 of 7



"k.vigneshwaran K" <vignesh_tooling@yahoo.co.in> wrote in message
news:kkl794$md6$1@newscl01ah.mathworks.com...
> Respected sir,
>
> your code is perfectly correct for getting KR Value with loop.
>
> but for each and every i value i ca get the KR value.that KR Values only i
> need in a specified name like that.please guide me to obtain sir.
>
> for i=1:2
> KR=i*K;
> end
>
> THE Above K is a 4 x 4 Matrix.
> HOW CAN I GET THE RESULTS LIKE FOLLOWING.
> if i=1
> my KR1=K

*snip*

DO NOT DO THIS.

See question 6 in the Programming section of the FAQ for an explanation why
this is a Bad Idea and alternatives you should use instead.

http://matlab.wikia.com/wiki/FAQ

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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