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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by