Checking Matlab indices exists, returning boolean value

91 ビュー (過去 30 日間)
Vijaya
Vijaya 2013 年 4 月 15 日
コメント済み: Guillaume 2016 年 2 月 24 日
Hi, is there any function to check, if a Matrix index exists or not. Like if(K(i,j))
should be zero if there is no value or the index does not exists, this works in C++, C# etc.. Is there any function in Matlab. Like I am accessing every pixel, and its 8 neighbourhood,
please suggest.
Regards

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 4 月 15 日
I'm pretty sure you cannot do that in C++.
It is not possible for there to be "no value" for a numeric matrix entry in MATLAB. It is possible for the entry to be NaN, though, provided that it is a floating-point datatype.
K(i,j) exists in MATLAB if "i" and "j" are positive, are integral, and if i <= size(K,1) and if j <= (numel(K) / size(K,1)). That last bit is probably unfamiliar to you: if K is a matrix with more than 2 dimensions then you can index it with two indices anyhow.
If you are sure that K is two-dimensional (which is often not the case for images), then the check becomes that i <= size(K,1) and j <= size(K,2)
There is no documented routine to do this kind of check.
  4 件のコメント
Vijaya
Vijaya 2013 年 4 月 15 日
Hi, The task is not applying mask, I would like to know which pixels have sum greater than three, Like a pixel surrounded by more than 2 active pixels (in black and white image ). And work on those pixels later.
Vijaya
Vijaya 2013 年 4 月 15 日
I am trying the function blockproc to process blocks, but it does not allow me to define my function.
here it is documentation B = blockproc(A,[M N],FUN) processes the image A by applying the function FUN to each distinct M-by-N block of A and concatenating the results into the output matrix B. FUN is a function handle to a function that accepts a "block struct" as input and returns a matrix, vector, or scalar Y:

サインインしてコメントする。


Zangdaarr
Zangdaarr 2016 年 2 月 24 日
function oValue = isIndexValid(iTable,iIndex)
try
if(isa(iTable,'cell'))
iTable{iIndex}; %#ok<*VUNUS>
else
iTable(iIndex);
end
oValue = true;
catch %#ok<CTCH>
oValue = false;
end
end
Just replace the inner code with an eval so you can pass your index as a string, and this function will work with any dimension table.
There is no native function in C/C++ to check this but you can do the same. However, it is generally not recommended to start try/catching null pointers or unknown allocated memory zone in C++, and exception are not really good for performances. You'd rather simply use containers that can return their size.
This is also what you should do in Matlab, however, since the point above is less an issue with Matlab, this function is actually cool to make code more readable.
  1 件のコメント
Guillaume
Guillaume 2016 年 2 月 24 日
Not sure why this is being revived two years later?
While exception handling should be part of any decent code, I don't agree that in this case it makes "code more readable". This:
oValue = iIndex < numel(iTable)
is in my opinion a lot clearer as to its purpose.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by