Clear Filters
Clear Filters

Preallocating matrix with a numeric trend

1 view (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 11 Jan 2016
Edited: Stephen23 on 11 Jan 2016
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));

More Answers (0)

Community Treasure Hunt

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

Start Hunting!