Preallocating matrix with a numeric trend

1 回表示 (過去 30 日間)
Brian
Brian 2016 年 1 月 11 日
コメント済み: Brian 2016 年 1 月 11 日
Hello all,
I manually wrote out the following matrix, IMiteration (since I didn't know how to code for it):
%%Define I1, I2 selection matrix (since not all combinations with higher
% keV at base is meaningful)
Mx1 = [40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 ...
45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 ...
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 ...
55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 ...
60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 ...
65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 ...
70 70 70 70 70 70 70 70 70 70 70 70 70 70]';
Mx2 = [45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 ...
50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 ...
55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 ...
60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 ...
65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 ...
70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 ...
75 80 85 90 95 100 105 110 115 120 125 130 135 140]';
IMiteration = [Mx1 Mx2];
Now, instead of having a step size of 5 in Mx2, I would like the step size to be 2 instead. So for example, the first 51 rows of the final product will have 40 in column 1, and 42:2:140 in column 2. The next 50 will have 42 in column 2, and 44:2:140 in column 2, and so on. Is there a smarter way to generate this matrix? Thanks.

採用された回答

Stephen23
Stephen23 2016 年 1 月 11 日
編集済み: Stephen23 2016 年 1 月 11 日
Here is a reasonably efficient solution:
U = 40:5:70; % values of Mx1
V = 45:5:140; % values of Mx2
M = numel(U);
N = numel(V);
Mx1 = repmat(U,N,1);
Mx1([1==triu(ones(M),1);false(N-M,M)]) = NaN;
Mx1 = Mx1(isfinite(Mx1));
Mx2 = hankel(V(1:M),[V(M:end),nan(1,M-1)]).';
Mx2 = Mx2(isfinite(Mx2));
  1 件のコメント
Brian
Brian 2016 年 1 月 11 日
Thank you! This is indeed efficient. :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by