필터 지우기
필터 지우기

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