How to use the function regexp?

2 views (last 30 days)
Monica S
Monica S on 11 May 2021
Commented: Monica S on 14 May 2021
I am working out problems from Cody and I have seen many top solutions are solved using 'regexp' funciton. Can anyone please explain me the functionality of regexp?
I have already read the matlab documents of this fucntion but still I am unable to understand certain things. For example, in problem 838 of 'Check if number exists in vector' where we need to check that number 'a' is present in the vector. Hence, if a = 2 and b = [1 2 3 4] and when use the command y = any( b(:) == a) it gives me logical output as 1. However, when I try the command regexp '' '(/@ y = any( b(:) == a))' it throws me an empty matrix as the result, why is this?
I don't get it where I am going wrong..?
Thanks in advance!
  5 Comments
Rik
Rik on 11 May 2021
Tricks in Cody tend to only be useful in Cody.
Good code in my view has several characteristics (not in a particular order):
  1. It is concise. Seperate tasks are split into functions that have a clear, single purpose.
  2. It is well-documented, containing a description of the goal, and the syntax description is only missing if it is blatantly obvious from the names of the variables.
  3. Each idea is commented (an idea can span a single line, or a short section like a loop).
  4. The code can be understood even without the documentation and comments: it uses meaningful names for variables and functions, instead of only using short names just to get to short code.
  5. It is fast.
  6. It is easy to extend (or can already handle more cases). An example would be a function processing a 2D image, but which already uses convn instead of conv2, so it is easy to extend to 3D images.
  7. It can run on many releases of Matlab and Octave.
Cody only attempts to score the first one. The rest is either penalized, or ignored:
  • Functions like cellfun and arrayfun should generally be avoided (with the possible exception of the legacy syntax of cellfun), but tend to get a better score on Cody.
  • Using ans as a variable name is (almost?) never clear naming. It is easy to get confused. Using actual variable allows you to be descriptive and explain what is happening.
  • Comments and documentation are completely ignored.
  • Performance is not taken into account at all, except for a handful of challenges where speed is the explicit goal.
  • Maintainability is not a factor in scoring.
Many of these are easy to understand: it is very difficult to come up with a method to objectively measure these.
You can't learn Matlab from Cody. You can learn Cody from Cody.
Monica S
Monica S on 14 May 2021
@Stephen Cobeldick & @Bob Thompson Thanks again for explaining the reg expression, @Rik: You took your valuable time to write about code and functionality of cody, thank you.
@Atsushi Ueno I didn't know this is dynamic regular expression, when I checked MatLab documentation I open the first link about regexp. Thanks, I learnt there is much more for me to check out.
P.S
Of course, Cody is not the platform to learn MatLab, I am using it test my analytical and programming skills.

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings 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!