How to use the function regexp?
4 views (last 30 days)
Show older comments
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
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):
- It is concise. Seperate tasks are split into functions that have a clear, single purpose.
- 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.
- Each idea is commented (an idea can span a single line, or a short section like a loop).
- 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.
- It is fast.
- 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.
- 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.
Answers (0)
See Also
Categories
Find more on Scope Variables and Generate Names 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!