# MATLAB problem

Joined Dec 29, 2004
83
Hi guys,

Please can anyone help me with this problem. It is an old homework and I want to practice.
This is it:

It may seem that we need the relational operators (<, >, etc.) to make decisions, but this is not necessarily true. The sign function can often be used to do the job. For example, suppose we want to compute the number of positive elements in a vector u. The sequence of commands w=sign(u)+0.5, v=fix(w); sum(v); will produce the desired value (try it!). Use this idea and define a Matlab function mycompare(u,v) that will compute the percentage of the components of the input vector u that are bigger than the corresponding component of the input vector v. You may assume that u and v are the same size vectors. Rememberno explicit loops or conditionals are allowed!

Thank you for helping me .

#### nanobyte

Joined May 26, 2004
120
Hi braddy. What programming language are you using? Is it C++? If so I would like to see all of your code for the program.

Joined Dec 29, 2004
83
Originally posted by nanobyte@Dec 29 2004, 06:31 PM
Hi braddy. What programming language are you using? Is it C++? If so I would like to see all of your code for the program.
[post=4342]Quoted post[/post]​
Hi, this is matlab program. it has its own language. You can find a (incomplete) list of command in :

http://www.engin.umich.edu/group/ctm/extras/commands.html

But what you can do, if you have an idea, is to give like an outline of what to do . I will try to trranslate it with Matlab commands.

Bertrand

#### Brandon

Joined Dec 14, 2004
306
Originally posted by braddy@Dec 29 2004, 03:25 PM
Hi guys,

Please can anyone help me with this problem. It is an old homework  and I want to practice.
This is it:

It may seem that we need the relational operators (<, >, etc.) to make decisions, but this is not necessarily true. The sign function can often be used to do the job. For example, suppose we want to compute the number of positive elements in a vector u. The sequence of commands w=sign(u)+0.5, v=fix(w); sum(v); will produce the desired value (try it!). Use this idea and define a Matlab function mycompare(u,v) that will compute the percentage of the components of the input  vector u that are bigger than the corresponding component of the input vector v. You may assume that u and v are the same size vectors. Rememberno explicit loops or conditionals are allowed!
Thank you for helping me .
[post=4329]Quoted post[/post]​
Z=v.-u ; .- does an element by element math function.
;if u>v then Z will be negative numbers (what we want)
;otherwise Z will be 0 or positive. (Discard)

Z=sign(Z); turn it into -1 0 or 1.
I=find(Z==1); I is an array of the indicies of all u which is greater than v
LenI=length(I); how many members of I
LenU=length(u); number of members of u
Percenage=LenI/LenU*100; formulate the percentage

the .- is either .- or -. I don't remember right at this moment as I am as work. But try both. do a [1 2 3].-[1 2 3]

Wen you get [0 0 0] you'll know where to put the .

There is the condition == in there so he may not accept it. He is another way.

once you get to the Z=sign(Z) we will have -1 0 and 1s.

Z=Z/2; -.5 0 .5
Z=ceil(Z); it either ceil or ceiling. So it rounds up. -.5 round UP to 0. 0 rounds to 0. .5 rounds up to 1
now you have all 0s and just 1s for the values you want.
Zsum=sum(Z);
Percent=Zsum/LenU*100;

I love MatLab. Use for DSP all the time.