Check Position with for loop, not enough input arguments

조회 수: 13 (최근 30일)
Anas Abou Allaban
Anas Abou Allaban 2015년 10월 6일
답변: Anas Abou Allaban 2015년 10월 7일
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
Anas Abou Allaban
Anas Abou Allaban 2015년 10월 6일
So I want to test if the location is taken (1) or not (0), how would I do that with the logical array as you stated without loops?
dpb
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... :)

댓글을 달려면 로그인하십시오.

채택된 답변

Anas Abou Allaban
Anas Abou Allaban 2015년 10월 7일
I answered my own question and changed the method to using an if/else statement.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 10월 6일
편집: Andrei Bobrov 2015년 10월 6일
checktakentest = @(x)any(x(:) == 1);
spotTaken = checktakentest(tttArray);

카테고리

Help CenterFile 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