Filter löschen
Filter löschen

Variable definition in a simple function

2 Ansichten (letzte 30 Tage)
Haojie
Haojie am 17 Jul. 2012
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Jul. 2012
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 Kommentar
Haojie
Haojie am 17 Jul. 2012
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by