Constructing a repeating array for a binary blazed diffraction grating

How can I build an array given the requirements below? The purpose of this is to construct a binary blazed diffraction grating
for an array NxM of A(i,j):
- for A(1,1), A(1,2), A(1,3) = 1 and A(1,4), A(1,5), A(1,6) = 0, repeat these 6 characters for A(1,M-5), A(1,M-4), A(1,M-3) = 1 and A(1,M-2), A(1,M-1), A(1,M) = 0.
- for A(2,1), A(2,2) = 1 and A(2,3), A(2,4), A(2,5), A(2,6) = 0, repeat these 6 characters for A(2,M-5), A(2,M-4) = 1 and A(2,M-3) A(2,M-2), A(2,M-1), A(2,M) = 0.
- for A(3,1) = 1 and A(3,2), A(3,3), A(3,4), A(3,5), A(3,6) = 0, repeat these 6 characters for A(3,M-5) = 1 and A(2,M-4), A(3,M-3), A(3,M-2), A(3,M-1), A(3,M) = 0
- Repeat the above 3 steps for N rows
i.e for a 12x12 array
A = [1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0]

 採用された回答

Star Strider
Star Strider 2015 年 11 月 7 日
Use the repmat function:
Element = [1 1 1 0 0 0;
1 1 0 0 0 0;
1 0 0 0 0 0];
A = repmat(Element, 4, 2)
This takes the ‘Element’ array and duplicates it to create 4 copies vertically and 2 copies horizontally.

4 件のコメント

Patrick Bevington
Patrick Bevington 2015 年 11 月 7 日
Can you think of a way to do the same thing using loops? Initially this code will be sufficient but for my final use I will be wanting to execute the same effect but on a non symmetrically repeating units (the 'lines' constructed by the values of 1 will curve).
Thank you
Star Strider
Star Strider 2015 年 11 月 7 日
My pleasure.
You could do it with loops easily enough, the easiest way being to vertically concatenate the ‘Element’ matrix with itself in the first loop, then duplicate it by horizontally concatenating the result of the first loop with itself, in the second loop.
I hesitate to write example code at this point because I don’t understand ‘(the 'lines' constructed by the values of 1 will curve)’. Can you give an example?
Patrick Bevington
Patrick Bevington 2015 年 11 月 9 日
編集済み: Star Strider 2015 年 11 月 9 日
Sorry I can see that my question was not clear, I meant can you think of a way to construct the repeating ' Element' matrix using loops?
The application of this is to make a saw toothed diffraction pattern, easy enough with a linear grating (example here, not yet sawtoothed: https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTsn5XzQAXdjDq6RffoF2yZOH_BEGAkZNqz2yEnwIzljOn1J8OV ), however I would like to do this for a forked grating (example here: https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTPyhsftJxNmhnfT2XG248q_sK0Q_kve_KLl9haWQoyqTZBLFPg ). I can provide code for creating either pattern if you think this would be helpful.
Thank you for your continued assistance.
Star Strider
Star Strider 2015 年 11 月 9 日
This is much less efficient than using repmat, producing the same result:
RowRepeat = 4;
ColRepeat = 2;
Grating = [];
for k1 = 1:RowRepeat
Grating = [Grating; Element];
end
for k1 = 1:ColRepeat-1
Grating = [Grating Grating(:,1:size(Element,2))];
end
Grating % View Result
The code would be helpful because I have no idea what you’re doing.
If you already have the code for what you want to do, why not just use it?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by