What is the shortest way to check the followings?

2 views (last 30 days)
  1. non-negative real scalar
  2. real valued vector having the length 2
  3. real valued numeric cell array containing 2 elements (length 2 also)

Answers (2)

Image Analyst
Image Analyst on 14 Jun 2015
Edited: Image Analyst on 14 Jun 2015
Hints: These functions may be useful isreal(), real(), length(), and isvector(). And, obviously the ">= 0" and "==" operations. I don't think you should need additional help beyond this.
  4 Comments
Jan
Jan on 14 Jun 2015
@Mr M: How do you define "shorter"? The run time of the command will not matter. Then the readability during debugging has the largest effect. Therefore the standard methods are optimal, although you might be able to create a cryptic command which is 1 or 2 characters shorter.
Image Analyst
Image Analyst on 15 Jun 2015
You said "my question was about any tricky method which is shorter or any predefined function which can be useful." You did not mention requiring a "tricky method" so I gave you the "predefined/built-in methods". Since you are now asking about tricky or predefined methods, then the predefined methods I listed should be sufficient. No need for tricks.

Sign in to comment.


Mr M.
Mr M. on 15 Jun 2015
One of my problem is that isreal() is not good! For example isreal('ab') is TRUE. Why?
  11 Comments
dpb
dpb on 17 Jun 2015
"Except that MATLAB doesn't see it that way..."
Precisely; that was my point re: "quality of implementation" (for lack of better nomenclature). That there are "issues" regarding memory on how it's used are certainly true but generally optimizers these days should be able to deal with what can be dealt with. If, otoh, the complex part is there, it's there and memory is required to deal with it (which may, depending upon how something is written, have the consequence one can't do as large a problem as one might if one took advantage of knowing a priori that the imaginary part is going to end up going away.
I'm not arguing strongly that it should be changed; simply noting that it fooled me after 30 year w/ Matlab as I was expecting it to be.
James Tursa
James Tursa on 17 Jun 2015
Once TMW embraced the philosophy of getting rid of identical 0 imaginary parts in calculation results, I think they were stuck. For the following two constructs:
a = 1 + 0i;
z = 0;
b = 1 + z*1i;
I think you could probably get a high percentage of votes that they should yield identical results for a and b (imaginary part physically removed), and another camp voting the opposite (the user typed in 0i explicitly so they must have wanted a complex result with 0 imaginary part physically present).

Sign in to comment.

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!