create matrix from loop in matlab

from the following code,
h=200; k=200;
for i=1:400;
for j=1:400;
a= round(atan2(h-i,-(k-j))*180/pi);
b(i,j)=[a];
end
end
how can I get (i,j) values for 0<= a <=5 because I want use those indexes to read elements from a "data(i,j)" matrix. Moreover, I want to get (i,j) for several ranges, say 5<= a <=10, 10<= a <=15,...85<= a <=90. How can I perform this?

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2017 年 8 月 14 日

1 投票

[ii,jj] = ndgrid(1:400);
x = [ii(:),jj(:)];
a = round(atan2d(200 - ii(:), jj(:) - 200));
idx = discretize(a,0:5:90);
t = ~isnan(idx);
xx = x(t,:);
aa = a(t);
out_idx = accumarray(idx(t),(1:numel(aa))',[],@(x){xx(x,:)});
Jan
Jan 2017 年 8 月 14 日
編集済み: Jan 2017 年 8 月 14 日

0 投票

The linear index is usually more efficient than the row and column indices:
% 0<= a <=5
index = (0 <= b) & (b <= 5);
But you can get the different indices also:
[i1, i2] = find((0 <= b) & (b <= 5));
For the set of ranges see
doc discretize

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

sb
2017 年 2 月 8 日

回答済み:

2017 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by