I have a complex matrix cdata
, that is 2144x2048
. I am getting elements from cdata
that are greater than a specified threshold
by doing the following:
[row, col] = find(abs(cdata) > threshold);
row
and col
can have multiple values. Then, I take the row and col values, I perform a calculation to get N samples of the real x-data, and 33 samples of the y-data as follows:
xdata = real(cdata(row(i),col(i)-bw:col(i)+bw))
ydata = real(cdata(row(i)-bw:row(i)+bw,col(i)-bw:col(i)+bw))
where bw
is a constant value that determines the number of samples I need to get. During this calculation, specifically the column portion of cdata
for the xdata
and the row portion of cdata
for the ydata
, I exceed the bounds of the matrix and MATLAB throws the following error:
??? Subscript indices must either be real positive integers or logicals
How can I ensure that I don't exceed the bounds? I'm ok with having to skip a row/col pair if it is going to exceed the bounds.
The reason you're having problems is because you're not restricting your search to closer then bw
from the edge of the matrix. This means its possible to find values above the threshold near the edges of the matrix. When you add or subtract bw
from these indices you end up out of bounds. You can restrict your search like this.
[row, col] = find(abs(cdata(bw+1:end-bw,bw+1:end-bw)) > threshold);
row = row + bw;
col = col + bw;
This guarantees your row and column indices are within the bounds so when you grab a region surrounding them you won't go out of bounds.
On a side note. The ydata
variable in your code is indexing an entire square region of the matrix and the xdata
is only indexing a section of a row. Should your ydata
actually be ydata = real(cdata(row(i)-bw:row(i)+bw, col(i)))
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.