MATLAB Answers

Making a script file that creates random vectors and include comment at top describing what the file does.

1 view (last 30 days)
John wick
John wick on 25 Aug 2019
Commented: John wick on 27 Aug 2019
This is the code I got should anything be adjust and what does it means by include a comment?
a = rand(1,6)
b = rand(1,6)
c = rand(1,6)
d = rand(1,6)
e = rand(1,6)
f = rand(1,6)
fprintf('%s\t',a,b,c,d,e,f)

  5 Comments

Show 2 older comments
dpb
dpb on 25 Aug 2019
Not certain what you mean by "adjust the command to make it look better", sorry.
You could look at what the syntax of using a semicolon at the end of each code line does, perhaps.
Also, your code still does not do the second task required to meet the requirement to "concatenate them horizontally, then display the result" (added emphasis mine).
The "more MATLABy" way would use looping and arrays or cell arrays, but your class may not have yet covered those topics so probably not something to worry about.
John wick
John wick on 25 Aug 2019
I mean if its were you what command would you use and adjust it to make it look more short and easy to understand. Do I need to use horzcat command? Am I supposed to use loop function to make it looks better?
dpb
dpb on 25 Aug 2019
I can't say what you're "supposed to do"; I'm not the instructor nor have I been in the class so I don't know what topics have been covered nor what particular topics were presented just prior to the given assignment. You'll have to judge for yourself from what you've been introduced to so far.
Again which command to use is your call; I don't want to unduly influence your answers by doing much more than making suggestions, but yes, you do need to create a variable that holds the full vector in one fashion or another to meet the second requirement of the assignment.
I was simply saying that one could use a loop and an array instead of the explicitly-named variables and a more experienced MATLAB-er would almost certainly do so if there got to be more than just a couple of instances.
If you have been introduced to looping, then I think I would recommend you investigate how you might do that--if you haven't actually covered the topic yet, then "not so much!".

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 25 Aug 2019
Hint:
To concatenate two row vectors into another, single vector, do
output = [rowVector1, rowVector2];
It will be easy for you to adapt this to your code to use 6 vectors instead of 2 (like I did).

  3 Comments

John wick
John wick on 26 Aug 2019
I'm really lost with this question I have no idea how to complete it. I really feel bad for not being able to do this one...
Image Analyst
Image Analyst on 27 Aug 2019
Did you try what I said? Here it is for 4 of them. If you can't figure out how to do it for all 6 after this, then I guess you'll just have to miss it
% This script does blah blah blah
a = rand(1,6)
b = rand(1,6)
c = rand(1,6)
d = rand(1,6)
e = rand(1,6)
f = rand(1,6)
fprintf('%.1f,\t',a,b,c,d,e,f)
output = [a, b, c, d]; % Add e and f to get all 6.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by