Thread Subject: simple calculation - where am I wrong?

Subject: simple calculation - where am I wrong?

From: N N

Date: 25 Apr, 2008 12:12:02

Message: 1 of 4

Could anyone help with the following calculation plse?


a = [2 4 20 3 6];
%number of attempts at each level, total 5 levels; for
example, first level was attempted 2 times

b= [1 .9 .5 0 1];
% percent correct at each level; for example, first level
was correct 100 percent times
 
for n=1:length(a);
    for m=1:length(b);
        if b(m) == 0
            b(m) = 1/(2.*a(n));
        elseif b(m) == 1
            b(m) = 1-(1/(2.*a(n)));
        else
            b(m) = b(m);
        end
    end
end
b

My aim is to get "b" without any 1s and 0s. Replaced
numbers totally depend on number of attempts (higher the
number of attempts, closer the "b" will be to "one"
or "zero".

With the above code, all the replaced numbers have been
calculated based on 1st element of “a” (think my code is
incorrect somewhere)

Current answer is the following:
b =
    0.75 0.90 0.50 0.25 0.75
(In this answer, last 2 calculations are incorrect)

Expected answer:
b =
    0.75 0.90 0.50 0.17 0.92

Help is much appreciated. Thanks.

Subject: simple calculation - where am I wrong?

From: david szotten

Date: 25 Apr, 2008 12:33:02

Message: 2 of 4

i may have misunderstood, but it seems you only want to loop
through once.

if you remove the outer loop and replace references to n, by
m, you get the answer you wanted

a = [2 4 20 3 6];
b= [1 .9 .5 0 1];
    for m=1:length(b);
        if b(m) == 0
            b(m) = 1/(2.*a(m));
        elseif b(m) == 1
            b(m) = 1-(1/(2.*a(m)));
        else
            b(m) = b(m);
        end
    end
b =

    0.7500 0.9000 0.5000 0.1667 0.9167

regards,
david


"N N" <cvlm_2005@hotmail.com> wrote in message
<fushqi$j4c$1@fred.mathworks.com>...
> Could anyone help with the following calculation plse?
>
>
> a = [2 4 20 3 6];
> %number of attempts at each level, total 5 levels; for
> example, first level was attempted 2 times
>
> b= [1 .9 .5 0 1];
> % percent correct at each level; for example, first level
> was correct 100 percent times
>
> for n=1:length(a);
> for m=1:length(b);
> if b(m) == 0
> b(m) = 1/(2.*a(n));
> elseif b(m) == 1
> b(m) = 1-(1/(2.*a(n)));
> else
> b(m) = b(m);
> end
> end
> end
> b
>
> My aim is to get "b" without any 1s and 0s. Replaced
> numbers totally depend on number of attempts (higher the
> number of attempts, closer the "b" will be to "one"
> or "zero".
>
> With the above code, all the replaced numbers have been
> calculated based on 1st element of “a” (think my code is
> incorrect somewhere)
>
> Current answer is the following:
> b =
> 0.75 0.90 0.50 0.25 0.75
> (In this answer, last 2 calculations are incorrect)
>
> Expected answer:
> b =
> 0.75 0.90 0.50 0.17 0.92
>
> Help is much appreciated. Thanks.
>

Subject: simple calculation - where am I wrong?

From: Steven Lord

Date: 25 Apr, 2008 13:26:11

Message: 3 of 4


"N N" <cvlm_2005@hotmail.com> wrote in message
news:fushqi$j4c$1@fred.mathworks.com...
> Could anyone help with the following calculation plse?
>
>
> a = [2 4 20 3 6];
> %number of attempts at each level, total 5 levels; for
> example, first level was attempted 2 times
>
> b= [1 .9 .5 0 1];
> % percent correct at each level; for example, first level
> was correct 100 percent times
>
> for n=1:length(a);
> for m=1:length(b);
> if b(m) == 0
> b(m) = 1/(2.*a(n));

At the first iteration through the N loop, if b(m) is equal to 0, you're
replacing it with 1/(2*a(1)). Unless a(1) is 0.5 or Inf, this means that
b(m) will not be 0 or 1 in any further iterations of your N loop, and so
will never be replaced again.

> elseif b(m) == 1
> b(m) = 1-(1/(2.*a(n)));

Similarly, unless a(1) is 0.5 or Inf, this will replace b(m) with a value
other than 0 or 1 in the first iteration of your N loop, so no further
iterations through that loop will modify this b(m).

> else
> b(m) = b(m);

You technically don't need this line, but I don't think it's hurting
anything.

> end
> end
> end
> b
>
> My aim is to get "b" without any 1s and 0s. Replaced
> numbers totally depend on number of attempts (higher the
> number of attempts, closer the "b" will be to "one"
> or "zero".
>
> With the above code, all the replaced numbers have been
> calculated based on 1st element of “a” (think my code is
> incorrect somewhere)
>
> Current answer is the following:
> b =
> 0.75 0.90 0.50 0.25 0.75
> (In this answer, last 2 calculations are incorrect)
>
> Expected answer:
> b =
> 0.75 0.90 0.50 0.17 0.92

Let me restate your aim, to make sure I understand. You want to replace
those elements of b that are equal to 0 with 1 divided by 2 times the
corresponding element of a. You also want to replace those elements of b
that are equal to 1 with 1 minus 1 divided by 2 times the corresponding
element of a. Correct?

If so, use logical indexing.

% Data
a = [2 4 20 3 6];
b= [1 .9 .5 0 1];

% Handle the b equal to 0 case
% locationOfZeros will be a logical array that we will use as a "mask" on a
and b
locationOfZeros = (b == 0);

% Now replace the elements of b corresponding to 1's in locationOfZeros
%
% Note I'm using locationOfZeros to index into a as well, to get the
elements
% of a corresponding to 0's in b. I also need to change 1/ to 1./ to
perform
% element-by-element division rather than matrix division.
%
% I don't need to use .* instead of * because 2 is a scalar, and matrix
multiplying
% with a scalar is the same as element-by-element multiplication. I could
use .*
% if I wanted to, though.
b(locationOfZeros) = 1./(2*a(locationOfZeros));

% Handle the b equal to 1 case similarly
% Minus (-) is already an element-by-element operator, so there's no .-
locationOfOnes = (b == 1);
b(locationOfOnes) = 1-(1./(2*a(locationOfOnes)));

The one case that would change between your code and my code is the a = 1/2
case, but since a is a number of trials, that shouldn't happen.

--
Steve Lord
slord@mathworks.com

Subject: simple calculation - where am I wrong?

From: N N

Date: 28 Apr, 2008 00:04:02

Message: 4 of 4

Thank you for your responses. Cheers!

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com