Info

This question is closed. Reopen it to edit or answer.

How can I tell if this is stored as a vector?

1 view (last 30 days)
Rachel Dawn
Rachel Dawn on 14 Feb 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
Basically, I created a function, which contains the following (along with some other stuff):
Lala= myfunction(a,b,n)
...
LaLa=zeros(1,n);
index= 3:n
Lala(index)= Lala(index-1) + Lala(index-2);
How can I tell if I'm just "displaying the series elements as I calculate them" or if I'm "storing and displaying the series as a vector"?
This is a homework assignment where they asked us to first display the first n numbers of the series, and then it says to 'modify the function' to return the series as a vector.
I've tried doing this: Lala= [1:length(n)]; (instead of the zeros function).
However, I don't know how to tell if Lala is a vector or not? It displays the same either way: 0 1 1 2

Answers (2)

Stephen23
Stephen23 on 14 Feb 2018
Edited: Stephen23 on 14 Feb 2018
"Displaying" in this context typically means to print some data to the command window. There are several ways to achieve this, such as:
  • any expression will display its output when it does NOT have a trailing semicolon.
  • calling disp with some input data
  • calling fprintf with some input data
  • error or warning calls.
So to fulfill the first part of the assignment "...first display.." you would need something like this:
function myfunction(a,b,n) % no output argument!
...
LaLa = zeros(1,n);
index = 3:n;
Lala(index)= Lala(index-1) + Lala(index-2) % no semicolon!
and for the second part you can add the semicolon (so it does NOT display any more) and add the output argument so that it returns those values:
function LaLa = myfunction(a,b,n) % with output argument!
...
LaLa = zeros(1,n);
index = 3:n;
Lala(index)= Lala(index-1) + Lala(index-2); %semicolon!
"However, I don't know how to tell if Lala is a vector or not?"
A vector has size 1xN or Nx1. According to that LaLa is a vector, because that is how you defined it using zeros(1,n). If n is a scalar then 1:length(n) is also a scalar (you did not tell us what size n is).

Rachel Dawn
Rachel Dawn on 15 Feb 2018
Wow, thank you so much! I wouldn't have thought it would be as simple as taking off a semicolon & the output argument. I thought it had to be something more complicated than that that I was missing.
You're a life saver!
  1 Comment
Stephen23
Stephen23 on 15 Feb 2018
@Rachel Dawn: I hope that it helped. You should also accept the answer that best helped resolve your original question: this tells other users that your question has been resolved.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!