|
"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
|