Got Questions? Get Answers.
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:
simple array relational problem

Subject: simple array relational problem

From: John Wong

Date: 3 Mar, 2011 06:40:21

Message: 1 of 3

Given an array n is all integers from 1 to 30, and array y is given by two conditions.
If n is divisible by 3,5,or 7, y is square root of n, otherwise, y is zero.

I tested with if-statements and array relational method, but the latter did not agree with the correct result (the if-statements is right in this case).

n = 1:30;
   y= (n.^(1/2)).*(mod(n,3)==0) + (n.^(1/2)).*(mod(n,5)==0) + (n.^(1/2)).*(mod(n,7)==0) + 0;
sum(y) % gives 79.3984

for n = 1:30
    if(mod(n,3) == 0)
        y(n) = n^(1/2);
    else if (mod(n,5)==0)
        y(n) = n^(1/2);
    else if(mod(n,7)==0)
        y(n) = n^(1/2);
    else
        y(n)= 0;
    end
    end
    end
end
sum(y) % gives 65.4656

I got the right answer by writing this little demo code
% z = [ 3 5 6 7 9 10 12 14 15 18 20 21 24 25 27 28 30]
% for n = 1:17
% g(n) = z(n).^(1/2);
% end
% sum(g)

My question is, where is my mistake??? Can anyone spot one?

Thank you!

Subject: simple array relational problem

From: Roger Stafford

Date: 3 Mar, 2011 07:15:22

Message: 2 of 3

"John Wong" wrote in message <iknd4l$her$1@fred.mathworks.com>...
> Given an array n is all integers from 1 to 30, and array y is given by two conditions.
> If n is divisible by 3,5,or 7, y is square root of n, otherwise, y is zero.
>
> I tested with if-statements and array relational method, but the latter did not agree with the correct result (the if-statements is right in this case).
>
> n = 1:30;
> y= (n.^(1/2)).*(mod(n,3)==0) + (n.^(1/2)).*(mod(n,5)==0) + (n.^(1/2)).*(mod(n,7)==0) + 0;
> sum(y) % gives 79.3984
>
> for n = 1:30
> if(mod(n,3) == 0)
> y(n) = n^(1/2);
> else if (mod(n,5)==0)
> y(n) = n^(1/2);
> else if(mod(n,7)==0)
> y(n) = n^(1/2);
> else
> y(n)= 0;
> end
> end
> end
> end
> sum(y) % gives 65.4656
>
> I got the right answer by writing this little demo code
> % z = [ 3 5 6 7 9 10 12 14 15 18 20 21 24 25 27 28 30]
> % for n = 1:17
> % g(n) = z(n).^(1/2);
> % end
> % sum(g)
>
> My question is, where is my mistake??? Can anyone spot one?
>
> Thank you!
- - - - - - - - -
  In your first method the integers 15, 21, and 30 each get counted twice because they are each multiples of two of the divisors 3, 5, and 7, not just one. That accounts for the difference in your results:

 79.3984 - 65.4656 = sqrt(15) + sqrt(21) + sqrt(30)

Roger Stafford

Subject: simple array relational problem

From: Jos (10584)

Date: 3 Mar, 2011 08:24:04

Message: 3 of 3

"John Wong" wrote in message <iknd4l$her$1@fred.mathworks.com>...
> Given an array n is all integers from 1 to 30, and array y is given by two conditions.
> If n is divisible by 3,5,or 7, y is square root of n, otherwise, y is zero.

Reading only this, I think you might be interested in learning about BSXFUN and the power of logical indexing:

n = 1:30
d = [3 5 7]

% these 2 lines can be combined
tf1 = bsxfun(@(x,y) rem(x,y)==0, n, d.')
tf2 = any(tf1,1)

% filling in the values
y = zeros(size(n)) % pre-allocation
y(tf2) = n(tf2)

~ Jos

Tags for 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