Compare numbers

  • Follow


Hi, I have variable "y"

y = 2

and I have cell array "k"

k = [1]    [2]    [4]    [5]    [6]    [7]    [8]    [9]

_______________________________________

what is the best way to get the information that "y" is equal to a certain index in "k".

I can't do it with strcmp, since it returns zeros to all 9 entries of "k".

Thank you.
0
Reply c 12/28/2009 7:07:04 PM

If your cell array is just individual numbers, why not convert it to a vector using cell2mat?
0
Reply Andy 12/28/2009 7:24:04 PM


Thank you, but that won't do it for me. Let me ask you this, then:

How do you convert those 9 doubles in the cell array to char's?

Thanks
0
Reply c 12/28/2009 7:49:04 PM

Who said anything about characters?  Indexes to arrays are numbers,
your "y" is a number, and your cell array appears to be numbers (since
there are no ' around them), so why are you talking about characters
now?
0
Reply ImageAnalyst 12/28/2009 7:56:00 PM

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2e65e1cb-ec77-46a0-a836-c11d196015a7@l2g2000vbg.googlegroups.com>...
> Who said anything about characters?  Indexes to arrays are numbers,
> your "y" is a number, and your cell array appears to be numbers (since
> there are no ' around them), so why are you talking about characters
> now?

Because I want to use strcmp...
0
Reply c 12/28/2009 8:07:04 PM

"c cc" <cruzadas@guerrillamailblock.com> wrote in message <hhb358$gf5$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2e65e1cb-ec77-46a0-a836-c11d196015a7@l2g2000vbg.googlegroups.com>...
> > Who said anything about characters?  Indexes to arrays are numbers,
> > your "y" is a number, and your cell array appears to be numbers (since
> > there are no ' around them), so why are you talking about characters
> > now?
> 
> Because I want to use strcmp...

In that case, I suppose you'll need a loop to convert each value to a string (num2str), then compare it to y, also converted to a string.

Doesn't seem logical to me to compare everything as strings, but to each his own. :o]
0
Reply Vince 12/28/2009 8:13:02 PM

On Dec 28, 3:07=A0pm, "c cc" <cruza...@guerrillamailblock.com> wrote:
> Because I want to use strcmp...


(Loud sound of hand slapping forehead)

0
Reply ImageAnalyst 12/28/2009 8:31:01 PM

Damn that's a whole lot of work. I think there should be a function.
0
Reply c 12/28/2009 8:35:20 PM

"c cc" <cruzadas@guerrillamailblock.com> wrote in message <hhb4q8$1n4$1@fred.mathworks.com>...
> Damn that's a whole lot of work. I think there should be a function.

That's because you're not comparing numbers, you're comparing strings. Strings are not numbers. Making a function to compare numbers as strings doesn't make sense, because those numbers can easily be compared... as numbers!... and then, if need be, converted to strings (for output).
0
Reply Vince 12/28/2009 8:59:04 PM

> I can't do it with strcmp, since it returns zeros to all 9 entries of "k".

Why?
You guys seem to be a cross-purposes.
Give an insight to why you want to use strcmp.
From your explanation, so far, I'm just not getting it. Tell us the things that makes it sensible to use strcmp.
0
Reply Neil 12/28/2009 9:06:04 PM

"c cc" <cruzadas@guerrillamailblock.com> wrote in message <hhb4q8$1n4$1@fred.mathworks.com>...
> Damn that's a whole lot of work. I think there should be a function.

Okay, let's start from the beginning again.  Why can you not use cell2mat?
0
Reply Andy 12/28/2009 9:09:04 PM

Damn guys, you all need to chill.

I needed to use strcmp because that's what my professor asked me to do, for studying the strcmp function purposes. Efficiency was not required.

Nevermind fellas. I didn't solve the problem. The deadline for my solution was reached. 

Thank you all anyway. Later!
0
Reply c 12/28/2009 10:34:04 PM

Dear c!

> Damn guys, you all need to chill.
> 
> I needed to use strcmp because that's what my professor asked me to do, for studying the strcmp function purposes. Efficiency was not required.
> 
> Nevermind fellas. I didn't solve the problem. The deadline for my solution was reached. 

I think, you did solve the problem!
You've learned, that the application of a tool, which does not match the problem, is a mess. Do not use STRCMP to compare numbers.
Do not use a can opener to start a car -- although it works with some fiddling.

But using FINDSTR to dig in a row-vector of doubles works fine! So in addition: Not every command containing "STR" is for strings only, but some are.

Kind regards, Jan
0
Reply Jan 12/29/2009 1:26:04 AM

"c cc" <cruzadas@guerrillamailblock.com> wrote in message <hhbbor$ikd$1@fred.mathworks.com>...
> Damn guys, you all need to chill.
> 
> I needed to use strcmp because that's what my professor asked me to do, for studying the strcmp function purposes. Efficiency was not required.
> 
> Nevermind fellas. I didn't solve the problem. The deadline for my solution was reached. 
> 
> Thank you all anyway. Later!

Well, just to give this a sense of closure, this is my best guess at what your professor was after:

kchar=cellfun(@(x) char(x), k, 'uniformoutput',0);

any( strcmp(char(y),kchar) ),
0
Reply Matt 12/29/2009 1:49:03 AM

13 Replies
291 Views

(page loaded in 0.105 seconds)

Similiar Articles:


















7/25/2012 3:49:58 AM


Reply: