Converting binary like table but using decimal values
古いコメントを表示
I'm trying to create a table that will start at a negative value and increment by one in the right most column and each column to the left will only increment when there is a carry. This is similar to a binary table or any other base-n table, but I would like to keep the values in decimal format, not alpha-numeric.
I had this code which was working well, but fails if I make 'n' > 5 due to the alpha-numerics once the decimal value reaches '10'.
m = 3; %Number of signals
n = n+1; %Number of harmonics
%% Harmonic Basis Matrix
T = zeros((2*n-1)^m,m);
for k = 1:(2*n-1)^m
T(k,:) = dec2base(k-1,2*n-1,m)-num2str(n-1);
end
When I subtract num2str(n-1) from the string '00A', the result is double('00A') = 17 - double('0') = 5 ==> 17-5 = 12.
I thought about creating the matrix in alpha-numeric string form and just splitting the values into columns, but couldn't figure out how to do that effectively without using cells - which I need to avoid as this will be used in some matrix algebra.
Please note, that the variables 'm' and 'n' are part of a function input and could be completely arbitrary.
Thanks for the help!
6 件のコメント
Jan
2019 年 5 月 6 日
Can you give us a short example for some inputs and outputs? I do not understand, what you are asking for.
Kyle Young
2019 年 5 月 6 日
Jan
2019 年 5 月 7 日
I assume, you mean (2*n+1)^m, not (2*n-1)^m.
Stephen23
2019 年 5 月 7 日
>> [X,Y] = meshgrid(-2:2);
>> M = [X(:),Y(:)]
M =
-2 -2
-2 -1
-2 0
-2 1
-2 2
-1 -2
-1 -1
-1 0
-1 1
-1 2
0 -2
0 -1
0 0
0 1
0 2
1 -2
1 -1
1 0
1 1
1 2
2 -2
2 -1
2 0
2 1
2 2
Jan
2019 年 5 月 7 日
m and n can be arbitrary. Then ndgrid is needed.
Kyle Young
2019 年 5 月 7 日
採用された回答
その他の回答 (2 件)
Walter Roberson
2019 年 5 月 6 日
0 投票
ndgrid or meshgrid and reshape to column vectors and put the columns together.
With the fast C-Mex function https://www.mathworks.com/matlabcentral/fileexchange/26242-vchoosekro
R = VChooseKRO(-2:2, 2)
Or:
n = 2;
m = 2;
len = (2*n+1)^m; % Not: (2*n-1)^m !
T = zeros(len, m);
v = repmat(-n, 1, m);
for k = 1:len
T(k, :) = v;
for im = m:-1:1
if v(im) < n
v(im) = v(im) + 1;
break;
end
v(im) = -n;
end
end
Or:
len = (2*n+1)^m; % Not: (2*n-1)^m !
T = zeros(len, m);
v = (-n:n).';
nv = numel(v);
r1 = nv ^ (m - 1);
r2 = 1;
for k = 1:m
T(:, k) = repmat(repelem(v, r1, 1), r2, 1);
r1 = r1 / nv;
r2 = r2 * nv;
end
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!