Variable definition in a simple function

2 次查看(过去 30 天)
Hi all, I have a very simple function and cant get it work. I try to let t0(1)=t(2), t0(2)=t(3)... and write this function below. However it will show an error when I type t0=resize(t); in the command window. I have already defined t in another function.
Error in ==> resize at 2
k=2;
??? Output argument "t0" (and maybe others) not assigned during call to
"D:\HJ\Documents\MATLAB\resize.m>resize".
function t0=resize(t)
k=1;
while (k<size(t));
t0(k)=t(k+1);
k=k+1;
end
Your help is much appreciated.

采纳的回答

Walter Roberson
Walter Roberson 2012-7-17
size(t) is always going to return a vector, so your expression k<size(t) is going to be comparing k to a vector of values.
An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false.
Therefor the "if" will be false if there is any dimension within t for which 1 < size(t) is false. Which would occur if t is a vector (row vector or column vector) as the other dimension of a vector is 1, and 1 < 1 is false.
If you examine your code, if the very first iteration of the "while" does not occur, then you do not create t0 at all.
I suggest you read about length()
  1 个评论
Haojie
Haojie 2012-7-17
Thank you. I am a newbie on matlab, so I didnt check carefully of the size function. Seems I need to do more study on the manual first

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by