Is there the more elegant way to do this?
23 views (last 30 days)
Jan on 11 Aug 2017
Edited: Jan on 11 Aug 2017
Not an answer, but a comment concerning the discussion:
This is another example for an inefficient debate: The group of experienced Matlab programmers provide profound arguments, and one person insists on claiming the opposite and suggesting it to beginners. Neither code examples, nor timings, nor references in Matlab's documentation can force somebody to change his opinion. But some contributors, as me, still spend time to post the important details to warn other beginners not to get trapped by the pitfall of eval.
There is a common sense about good programming practices. Voting for good solutions helps to share this knowledge. Currently there are 9 votes for the good solution, and 0 vote and 1 acceptance for a bad solution. All readers can draw their own conclusions. The only problem is, that the accepted status has such a prominent position and shiny green check mark.
The editors can un-accept an answer, but they use this power very rarely for good reasons. The drawback for the forum would be reduced, if e.g. 4 votes (of maybe MVP members) move an answer on top of the accepted one.
It will not be possible to convince all forum members to suggest good programming practices. Discussing the same point with the same person again, will not help. What can we do instead to support the quality of the forum's contents?
John BG on 10 Aug 2017
Edited: John BG on 11 Aug 2017
Hi Mukesh Kumar
this is John BG <mailto:firstname.lastname@example.org email@example.com>
Elegance like beauty lives in the eyes of the observer, so while some people abhor the command evalin, I consider it's a really powerful and elegant option to simplify the generation of variables.
-2 4 -10
3 -10 -8
-7 -5 7
>> A2=randi([-10 10],3)
4 -10 6
-4 -1 6
9 -2 -7
>> A3=randi([-10 10],3)
0 4 4
-1 5 3
3 -5 -7
2. Code that writes code
str1=['E' num2str(k) '=sum(A' num2str(k) ')'];
-6 -11 -11
9 -13 5
2 4 0
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance