主要内容

Results for


I started with Cody today and I found the file calculateSize.m https://de.mathworks.com/matlabcentral/fileexchange/34754-calculate-size which should determine the size of my solution. Unfortunately I do not get it to run. Can someone explain this to me or maybe explain how exactly the size of the solution is calculated?
Summary:
Dynamically accessing variable names can negatively impact the readability of your code and can cause it to run slower by preventing MATLAB from optimizing it as well as it could if you used alternate techniques. The most common alternative is to use simple and efficient indexing.
Explanation:
Sometimes beginners (and some self-taught professors) think it would be a good idea to dynamically create or access variable names, the variables are often named something like these:
  • matrix1, matrix2, matrix3, matrix4, ...
  • test_20kmh, test_50kmh, test_80kmh, ...
  • nameA, nameB, nameC, nameD,...
Good reasons why dynamic variable names should be avoided:
There are much better alternatives to accessing dynamic variable names:
Note that avoiding eval (and assignin, etc.) is not some esoteric MATLAB restriction, it also applies to many other programming languages as well:
MATLAB Documentation:
If you are not interested in reading the answers below then at least read MATLAB's own documentation on this topic Alternatives to the eval Function, which states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array." Data in a single array can be accessed very efficiently using indexing.
Note that all of these problems and disadvantages also apply to functions load (without an output variable), assignin, evalin, and evalc, and the MATLAB documentation explicitly recommends to "Avoid functions such as eval, evalc, evalin, and feval(fname)".
The official MATLAB blogs explain why eval should be avoided, the better alternatives to eval, and clearly recommend against magically creating variables. Using eval comes out at position number one on this list of Top 10 MATLAB Code Practices That Make Me Cry. Experienced MATLAB users recommend avoiding using eval for trivial code, and have written extensively on this topic.
Something seems to either have changed with webread/urlread behavior with R2016a or the Cody site has changed its authentication after it's September 2016 upgrade. Now, when ever I make a call using either webread (with weboptions) urlread using "My Cody" url, I get the html that corresponds to an unauthenticated user.
I've tried passing credentials using weboption and also cached the credentials using the built in Matlab browser - neither helped in seeing the authenticated version of this page.
Any help in understanding where the problem/solution lies would be helpful. Easily reproduced by running...
URL = 'https://www.mathworks.com/matlabcentral/cody/players/1261697-gregory/solved';
[Contents Status] = urlread(URL);
Compare this to the source pulled down when authenticated with Chrome (for example, search for the work "two"). FYI: this has worked for a couple of years up until now...
Inspired by Chad Greene's " MATLAB jokes or puns " thread, and in celebration of 15 years of the MathWorks Community site, does anyone out there want to share their poetic creativity? Limericks, haiku, sonnets... Go!
And to start off, my (slightly off-topic) submission on Chad's thread:
There was an old math guy called Cleve
who, while teaching, a pipe-dream conceived:
of a language so clean
you can say what you mean!
From our suffering we've all been relieved.
O45
O45
Last activity 2018-10-23

Hi, I am uploading the answers to cody problems. Although most of answers are correct but my size is bigger than the best answer. How can I view the best answer?
Edit: due to the increasing size of this thread, it is continued here.
What should you post where?
Wishlist threads (#1 #2 #3 #4 #5 #6): bugs and feature requests for Matlab Answers
Frustation threads (#1 #2): frustations about usage and capabilities of Matlab itself
Missing feature threads (#1 #2): features that you whish Matlab would have had
Next Gen threads (#1): features that would break compatibility with previous versions, but would be nice to have
@anyone posting a new thread when the last one gets too large (about 50 answers seems a reasonable limit per thread), please update this list in all last threads. (if you don't have editing privileges, just post a comment asking someone to do the edit)
This topic is for features you would like to see for the MATLAB Answers facility itself, and also for bug reports about the MATLAB Answers facility.
This topic is the follow on to the first Wish-list for MATLAB Answer sections and second MATLAB Answers Wish-list #2 (and bug reports). Those grew large enough to become unwieldy; and Mathworks has made enough changes to make a number of the past points no longer of relevance. More recently there was the limited purpose New design of the forum - grey on white which turned into a bug and wish list; I have renamed that for continuity.
I suggest one wish (or bug report) per answer, so that people can vote their wishes.
The community is very helpful, yet I feel really powerless that I cannot find the appropriate way to code, nor find the problems with the codes I have written. I have read numerous books on MATLAB, mostly related with science and engineering applications. Any advice to improve would be greatly appreciated. Thanks.

Apologies for putting this question here, but I'm not sure there's a specific forum for Cody-related questions.

I recently noticed that a new badge for "Magic Numbers Master" had been created and awarded. When I entered my profile to check that out, I noticed that I had received that badge, but lost the badge for "Cody Challenge Master." I thought that maybe my solution had been messed up because of a change in one of the test suites that my solution missed, as this has happened a number of times on other problems. According to the text underneath the badge icon, I've still solved all 96 of the questions in the Cody Challenge. All of the problems listed as part of that challenge are still marked as "Solved." Has anyone else run into this issue?

Chad Greene
Chad Greene
Last activity 2023-9-12

Are there any good Matlab jokes? I don't mean why or any other Easter eggs, I mean good jokes involving Matlab. Actually, that bar may be a bit too high. Any jokes, good or bad, let's hear 'em.
NOTE: this discussion is continued at MATLAB Answers Wish-list #6 (and bug reports)
I've opened MATLAB Answers this morning and found the new design.
The field for typing the "Body" does not consider the font settings of my browser anymore, such that my preference of sans-serif fonts is ignored. In addition the text color is a medium gray, which is hard to read for me due to the too light contrast.
Blank lines in the code let two separate code boxes appear. This makes almost all code, I've posted in the forum, invalid. It has been discussed repeatedly, that blank lines in the code confuse the indentation of the display in the forum and that this is a really bad idea. But instead of improving this, it is made severely worse now.
The new design contains even more white space, such that standard questions cannot be answered without extensive vertical scrolling. It is a very bad drawback, that I cannot see the question while I type the answer.
There is still no suggestion to use a proper code formatting, such that I have to spend 20% of my forum time typing corresponding comments as before.
But I'm coming back to the most important problem for me: It is a physical problem for me to read the low contrast grey on white text. Does anybody know a tweak or CSS trick to increase the readability?
TMW, please take into account that this new design is physically hard to read for people without young and 100% perfect eyes. This is very annoying for me.
Splitting code blocks at white lines is simply a bug. I cannot imagine why this error has not been detected before the new design has been published. The argument, that TMW is extremely conservative with changes in the forum to ensure a stability does not convince me anymore.
[EDITED] The box around the thread, the preview box, the boxes for preformatted text and code have a grey background now. So some text is even medium grey on light grey.
I'd be glad if the designers refocus on the purpose of the forum. Whatever this purpose might be, the optical reception of the characters is fundamental.
I created a solution for Cody Problem 1745 (Get me!) that is very simple and works perfectly on my computer; but in Cody, it results in the error:
Error: Undefined function or variable 'me'.
The test code is:
get = me();
y = rand(1,12345);
!rm now.m
!rm assert.m
assert(get == now)
My solution is:
classdef me
methods
function tf = eq(obj,~)
tf = true;
end
end
end
Why doesn't Cody accept this?
(P. S.) And why doesn't it accept function definitions like
function justDoIt
(which are appropriate for some problems and accepted by MATLAB) but requires at least one input and output?
Can anyone explain how Cody calculates the size of a program?
I read the help, but couldn't understand. :(
Why cody does not allow 'eval', 'evalc','feval' etc functions in the solutions? What is alternative to these functions?
e.g. I need to use:
if f='3*x'
x=0:10;
y=eval(vectorise(f))
so that values of y can be calculated as 3.*x
bt cody does not let us to use functions like eval
What is the alternative?
I have been stuck in the Cody question: Problem 31. Remove all the words that end with "ain"
for the whole afternoon. My codes below passed the first two tests but failed in the third one, because my codes cannot separate "" and ain. Anyone could help?
function s2 = remAin(s1)
s1_cell = strread(s1, '%s');
[s1_cell{find(not(cellfun('isempty', regexp(s1_cell, '\w*ain\>'))) == 1)}] = deal(' ');
s2 = strjoin(s1_cell');
s2 = strrep(s2, sprintf('%c%c%c', 32,32,32), sprintf('%c%c', 32,32));
if s2(end) == ' ' && s2(end-1) == ' '
s2(end) = [];
end
end
What MATLAB tools or functions have you been thinking about making but haven't quite gotten around to it?
It can be something specific to your work if you'd like, but it's preferably:
  • the kind of tool or task that we don't really expect in the next MATLAB release (ie., not a frequent entry in a MATLAB wishlist)
  • something that would be useful to you and (hopefully) others if it existed.
  • something that could conceivably be made by people(s) in the user community
Think of it maybe as a file exchange wishlist.
Votes for good ideas . I guess you'll get more votes if it's an idea others want to see made as well.
Probably no single accepted answer, but it would be nice to see what people have thought about and maybe we can bring a few ideas to fruition (or find that someone's already done it)
For example:
  • I've been wanting to write an object-oriented replacement for the NURBS toolbox which is a great toolbox but is very unwieldy to use.
Salam,
While I was playing with Cody, I found one interesting question that asked to write MATLAB function to create times-table (can be found here: http://bit.ly/1cWZGGM ). The question itself was easy, but I had problems to figure out how could someone solve this problem with compact code of size 10 !!
After I've checked the leading answer, it seems to be written in Chinese not in MATLAB :)
Here is the answer:
function m = timestables(n)
regexp '' '(?@A=repmat([1:n]'',1,n);m=A.*A'';)'
end
Could anyone translate this to me so that I can use it in my future attempts ? :P
Regards.
In a previous Q & A, Jan Simon pointed to Cody: Sum 1:2^n. The current leading solution to that problem has node-count (or more simply, "length") 10. Apparently, 10 is the minimal length (per the official length-function on File Exchange) of any function taking input & generating output:
function y = test_cody_solution(x)
y = x;
end
Per Cody instruction examples, additional computation within a function definition increases the solution length. For example, both of the following functions have length 12:
function y = test_cody_solution(x)
y = [x];
end
function y = test_cody_solution(x)
y = x+1;
end
My question is: what kinds of ninja-style coding idioms even exist in MATLAB which actually perform definite computation but at the same time do not increase the node-count above 10? I'm not able to imagine what could be going on in order for someone to solve a given non-trivial Cody puzzle in length 10 or 11? IOW, without respect to any particular Cody problem, could someone please give an example of a non-trivial function which somehow comes in at or just above the absolute lower bound? Any explanation of the magic would be appreciated as well.
Thanks, Brad
function b = most_change(a)
a(:,1)=a(:,1)*0.25;
a(:,2)=a(:,2)*0.1;
a(:,3)=a(:,3)*0.05;
a(:,4)=a(:,4)*0.01;
d=sum(a,2);
c=max(d);
for i=1:length(d)
if d(i)==c
b=i;
end
end
i got wa please explain idont understand
I would like to receive your feedback:
  • would you find useful to have spellcheck built into the MATLAB Editor?
(Consider also an associated toggle, positioned somewhere in the Editor, that would enable/disable the spellcheck for selective use)
Lately, I am writing many quick analyses for publish() and I find that first publishing to MS Word (to check the spelling) and then re-publishing to .html, after manual corrections, disrupts the working.
IMHO, the idea to include spellcheck into the editor would be consistent with the renewed visibility of the PUBLISH tab.