- As you show it, you've thrown away the result of the routine so there wasn't any point in calling it to begin with-- use a return variable, and
- Do NOT use a global array for the data array; instead compute it wherever it is it needs be but again use an array in which to return its new value (can be the same as that with which you call it) and pass that to any other function(s) that need it.
- As you've written it, j is undefined in the first loop; variables are local within the scope of functions excepting for (ugly!) global ones which are as noted to be avoided if at all possible. Hence, defining the loop variable outside the function has no bearing; those h, j are entirely different variables than those using the same names inside the function. Besides, when you create the for loop, even if they were in the function they're being re-defined.
Check Position with for loop, not enough input arguments
1 次查看(过去 30 天)
显示 更早的评论
I made a simple function that loops between the rows and columns of an array using for loops. The loop is part of a function named checktakentest (Since I'm testing this method atm). I keep getting the error that there aren't enough input arguments.
function [spotTaken] = checktakentest(tttArray)
for h = 1:3
if tttArray(h,j) == 1
%Is spot is taken, break loop
spotTaken = 1; break;
else
spotTaken = 0;
end
for j=1:3
if tttArray(h,j) == 1
spotTaken = 1; break;
else
spotTaken = 0;
end
end
end
I tried also defining h and j previously as follows
h = [1,2,3];
j = [1,2,3];
Note that tttArray is a global variable defined in another function and its array values change in that function. A spot taken is 1, empty is 0. What arguments should I pass to the function and how do I know which ones to pass since this has been a recurring problem for me? A simple explanation would be appreciated. Note that I call the function via
checktakentest(tttArray)
3 个评论
dpb
2015-10-6
isTakenIJ=(tttArray(i,j)==1); % test single element
Since you're keeping just 0/1 values, even though it's not a logical array you can simply use
isTakenIJ=(tttArray(i,j)==1); % test single element
Again, precisely what might work best depends on what you're really after in the calling routine; you've got an array that contains 0|1 and you're creating another either array or single value that's also 0|1 in the same location(s). That seems at least somewhat redundant... :)
采纳的回答
更多回答(1 个)
Andrei Bobrov
2015-10-6
编辑:Andrei Bobrov
2015-10-6
checktakentest = @(x)any(x(:) == 1);
spotTaken = checktakentest(tttArray);
0 个评论
另请参阅
类别
在 Help Center 和 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!