忍者ブログ
Yaleで、遊んで学ぶ日々。

Yaleで、遊んで学ぶ日々。

囲碁、ときどきプログラミング、ところにより経済。
[232]  [231]  [230]  [229]  [228]  [227]  [226]  [225]  [224]  [223]  [222
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Matlabプログラミングの問題が出されて、答えを書くとその長さに応じて得点が入る。短いほど高得点らしい。
http://www.mathworks.com/matlabcentral/cody/


今日はこれを考えてみる(ソース)。

INSTRUCTIONS

This is a multi-dimensional variant of the normal multiplication table used to teach elementary students multiplication. In this variant, we are going to produce a table that multiplies the divisors 1 to n (input) agains itself in d dimensions.

Note: Inputting d = 0 should return the number 1 and d = 1 should return a column vector with the elements 1 to n.

Example:

Input:

n = 3;
d = 3;

Output:

tt(:,:,1) = [ 1  2  3
              2  4  6
              3  6  9  ];
tt(:,:,2) = [ 2  4  6
              4  8  12
              6  12 18 ];
tt(:,:,3) = [ 3  6  9
              6  12 18
              9  18 27 ];

これで一応dが2以上の場合に対応できるようだが、forループ使っちゃってるし、d=1,0の場合を別に書かなきゃいけないからあんまり美しくない。ループなしで書く手もありそうだが・・・。


n = 3;
d = 3;
 
% --------------------------------------------------------------------
order = 1:d;
A = repmat((1:n)', [1, n * ones(1, d - 1)]); 
OUT = repmat(1, n * ones(1, d));
for dd = 1:d
    oo = (1 == dd) * order + (1 < dd) * ( (order == 1) * dd + (order == dd) * 1 + (order ~= 1 & order ~= dd) .* order);
    OUT = OUT .* permute(A, oo);
end
 
OUT
% ---------------------------------------------------------------------


 
PR
この記事にコメントする
お名前:
タイトル:
文字色:
メールアドレス:
URL:
コメント:
パスワード:   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
Calender
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Search in This Blog
Latest Comments
[03/30 川内のばば山田]
[03/30 川内のばば山田]
[08/06 Aterarie]
[07/05 Agazoger]
[07/01 Thomaskina]
Oldest Posts
Latest Trackbacks
フリーエリア

Barcode
Access Analysis
Powerd by NINJAブログ / Designed by SUSH
Copyright © Yaleで、遊んで学ぶ日々。 All Rights Reserved.
忍者ブログ [PR]