Test for within cell array boundaries
2 次查看(过去 30 天)
显示 更早的评论
Is there a test for whether or not a cell is within a cell array's boundaries?
For example, myarray = cell(8);
inbounds(myarray{8,8})
1
inbounds(myarray{9,9})
0
Is there an actual function equivalent to my inbounds?
0 个评论
采纳的回答
Andrei Bobrov
2017-5-27
编辑:Andrei Bobrov
2017-5-27
inbounds = @(myarray,indexes)all(size(myarray) >= indexes(:)');
use:
>> inbounds = @(myarray,indexes)all(size(myarray) >= indexes(:)');
myarray = randi(125,10,8);
out = inbounds(myarray,[2 6])
out =
logical
1
>>
0 个评论
更多回答(1 个)
Jan
2017-5-27
编辑:Jan
2017-5-27
myarray = cell(8);
search = [9, 9];
if all(search <= size(myarray))
... existing
You can create such a function easily:
function ok = inbounds(X, Index)
if numel(Index) == ndims(X)
ok = all(Index > 0) && all(Index == round(Index)) && all(Index <= size(X));
elseif numel(Index) == 1
ok = (Index <= numel(X));
else
ok = false;
end
You could or should expand this to consider the convention that trailing singelton dimensins are ignored in Matlab:
X = rand(2,3);
X(2,2,1,1,1,1,1) % Ok!
An explicite test inside the code might be smarter than such a general method.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Direct Search 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!