Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Rachel Dawn
Rachel Dawn 2018 年 2 月 14 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (2 件)

Stephen23
Stephen23 2018 年 2 月 14 日
編集済み: Stephen23 2018 年 2 月 14 日
"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 2018 年 2 月 15 日
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 件のコメント
Stephen23
Stephen23 2018 年 2 月 15 日
@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!

Translated by