Answer to Question #2996 in MatLAB | Mathematica | MathCAD | Maple for geminia

Question #2996
Hi

I just need a little help with matlab

say if you have an array [2 5 10]

and you have a result from a different function say y=8

and want to find which numbers of the array have the same highest binary digit with y in the case
above y=[1000] and 2=[0010] 5=[0101] 10=[1010] so the only number in the array which has the same highest binary digit with 8 is 10.

How can i do this in matlab?
1
Expert's answer
2011-06-07T09:28:42-0400
function mx=q2996(y,x)
n=floor(log2(y)+1);
l=length(x);
mx=[];
for k=1:l
if floor(log2(x(k))+1)==n
mx=[mx x(k)];
end;
end;

Example:

mx=q2996(8,[2 5 10 12 16 18])

mx = 10 12

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS