Preallocating matrix with a numeric trend
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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));
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!