Why is this code "running without output"?

This code gives me no output... with the input of (4, [10, 20, 30, 40]), I am expecting the output of [30, 50, 70, 40]. Although I think this is the incorrect code to give me the expected output anyways, I am wondering why it does not give me any output matrix at all. Why does this code give the error, "This code ran without output"?

function bonusScores = CombineScores(numberScores, userScores)
   for i = (1:numberScores-1);
     userScores(i) = userScores(i) + userScores(i+1);
   end
end

回答(2 个)

Matlab's editor has live syntax highlighting and error checking. When you write code in the editor, it shows you what can possibly be wrong with it. Each squiggly underline and orange square on the right is a potential mistake. Why do you ignore it?
When I paste your code in the editor, it highlights to problems: a minor unnecessary semicolon and the fact that the function output bonusScores is never assigned anything. If it were up to me, it should also highlight that the brackets around 1:numberScores-1 are also unnecessary.
In addition, if when you run the function it runs without error but doesn't give you an output, that would be because you didn't ask for an output, i.e. you ran the function with for example:
>> CombineScores(4, [10 20 30 40])
If you'd try to run the function and asked for its output, e.g.
>> result = CombineScores(4, [10 20 30 40])
Output argument "bonusScores" (and maybe others) not assigned during call
to "CombineScores".
then you'd have got the error shown above since your function never assigns anything to its output.
Finally, I have no idea what you're trying to do with your function. Note that there is no point passing the number of elements of an array together with the array. Matlab already knows how many elements there are in the array. In addition, the loop is unneeded, you could rewrite the code as:
csum = cumsum(userScores(1:numberScores));
userScores(1:numberScores-1) = csum(2:end);
This code is not running..no output..Please help
syms p
f = 1/p^2;
function ilt=gavsteh(f,t,L)
N = L/2;L=8;
for n = 1:L
z = 0.0;
for k = floor( ( n + 1 ) / 2 ):min(n,N)
z = (-1)^(n+N)*{z + ((k^N)*factorial(2*k))/ ...
(factorial(N-k)*factorial(k)*factorial(k-1)* ...
factorial(n-k)*factorial(2*k - n))};
end
v(n)=z*((-1)^(n+N));
end
sum = 0.0;
ln2_on_t =log(2.0) / t;
for n = 1:L
p = n * ln2_on_t;
sum = sum + v(n) * feval(f,p);
end
ilt = sum * ln2_on_t;
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by