MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

In MATLAB, you create a vector by enclosing the elements in square brackets like so:

x = [1 2 3 4]

Commas are optional, so you can also type

x = [1, 2, 3, 4]

Create the vector

x = [1 2 3 4 5 6 7 8 9 10]

There's a faster way to do it using MATLAB's colon notation.

13706 correct solutions
3912 incorrect solutions

Last solution submitted on May 23, 2015

25 players like this problem

1 player likes this solution

1 Comment

Wang Yunhe
on 16 Mar 2015

I do not deem that these problems can enhance one's performance.....

1 Comment

Matthew Caron
on 7 Jan 2015

This question could have been a bit more challenging.

2 Comments

Christopher Raabe
on 4 Jun 2014

why is this incorrect?

Tim
on 5 Jun 2014

You need to insert "function x=oneToTen" as the first line of your function.

1 Comment

Ahsan Zamee
on 31 Dec 2013

give a value of y simple calling the function oneToTen. it will definitely work

1 Comment

Kirk
on 17 Dec 2013

I do not understand the point of Cody... I cannot use "eval 1:10;" but I can use "str2num 1:10;" which is overhead + eval 1:10... Please make Cody evaluate the functions used in submitted code

1 player likes this solution

2 Comments

li
on 8 Jul 2013

what's the "str2num" mean?

Tim
on 8 Jul 2013

str2num means "convert string to number"; even more cryptic is the fact that "str2num 1:10" is equivalent to "str2num('1:10')" (Matlab allows you to omit the single quotes and parentheses when using functions that take character strings as arguments).

1 Comment

POORNAKUMAR
on 14 Mar 2013

please unlock the solution

9 players like this solution

4 Comments

Show
1 older comment

John Kotrosa
on 19 Nov 2012

I’ve found that the huge strength of MATLAB is that it frees me to rapidly prototype numerical math problems – before I design a targeted solution. Such esoteric efficiencies as above are very clever and much credit goes to the person who presented it, however, this distracts rather than adds significant value to my use of MATLAB. Depending on the target machine (Desktop workstation or embedded device) I will ultimately use C/C++, assembly and VHDL to implement my process efficient code – never an interpreted language.

John Kotrosa
on 5 Dec 2012

This is an update since I had one person ask about my post...
I believe that speed is only one metric of performance, another is memory utilization. The key in this problem is the “type” of the variable stored internally. By using ans=1:10; the variable ans is (I believe) of the type double precision floating point (8 Bytes for each number). On the other hand by using str2num, a cast to an array of Characters (1 Byte each) is used to tightly store the tiny integer range of numbers needed. Thus the memory required to store ans is 8 times less.
SO, I’m not sure how they actually score (or weight) the importance between memory efficiency and execution time (and across multiple platforms and OS’s) since in your case you claim a faster execution time without the casting. This would need to be asked to a Mathworks person with specific insight to their code – sorry.
And back to my soapbox point… I’m using a machine that has 12 GB of RAM, so the difference between using 10 bytes or 80 bytes is not important to me.
HOWEVER, understanding this issue IS important so my hat is off to the Cody folks for the great (and fun) challenge!
Hope that helps!
john

Óscar
on 7 Dec 2012

The metric used to evaluate the performance of a solution is http://www.mathworks.com/matlabcentral/fileexchange/34754-calculate-size
As you can see if you inspect the code, it has nothing to do with speed or memory. That is why I proposed this solution, in order to show that you can become the leader with a code that is not efficient at all, either in execution speed or in memory use. Although I think the idea of Cody is great and challenging I believe that the metric that is used nowadays should be rethought.

HaveF
on 17 Dec 2012

interesting

2 players like this solution

1 Comment

John D'Errico
on 1 Feb 2012

cumsum(ones(1,10))

2 Comments