problems with creating a multiplication table

7 ビュー (過去 30 日間)
Tom Schneider
Tom Schneider 2022 年 8 月 9 日
コメント済み: Steven Lord 2022 年 8 月 11 日
I want to make a function which creates a multiplication table with the dimensions C for Collumns and R for Rows. After creating the basic multiplication table the whole table has to be converted to a certain base b.
The code works as far as you use a method calls such as "multTable(7,3:10,2:3:33)". However i cant seem to find any good implementation that accepts method calls such as multTable(7,10,33). I tried checking to see if the R and C variable has zero, one or two ":" in them and then adding "1:" to the start of them but that didnt really work.
function y = multTable(b, R, C)
left = (R)';
top = (C);
table = (R)' * (C);
table = [left table];
top = [b top];
table = vertcat(top,table);
w = width(table);
h = height(table);
for i= 1:h
for ii = 1:w
a = dec2base(table(i,ii),b);
table(i,ii) = str2num(a);
end
end
table(1,1) = b;
y = table;
end

採用された回答

DGM
DGM 2022 年 8 月 10 日
編集済み: DGM 2022 年 8 月 10 日
R and C have no ':' in them. R and C are row vectors, because that's what 3:10 and 2:3:33 are. It's unclear what the behavior should be if R or C is specified as a scalar, so that's something you'll have to decide on.
Once you figure out how you want to handle scalar inputs, test R and C using isscalar(), and then handle those cases as you see fit.
You might also want to be wary of how you presume the input vectors to be oriented. If you want to assume that vector inputs are always row vectors, then make sure to clearly document that requirement when you write the function synopsis.
Also, you do realize that this won't work for any numeric base >10, right?
table(i,ii) = str2num(a);
This will fail if the variable a contains any non-numeric characters. If you want to handle those cases, then the output array cannot be numeric.
Consider the example wherein I make some gross assumptions about intent:
% test inputs
R = 1:4;
C = 1:4;
b = 16;
% try the thing
mytable = multTable(b, R, C)
mytable = 5×5 cell array
{'16'} {'01'} {'02'} {'03'} {'04'} {'01'} {'01'} {'02'} {'03'} {'04'} {'02'} {'02'} {'04'} {'06'} {'08'} {'03'} {'03'} {'06'} {'09'} {'0C'} {'04'} {'04'} {'08'} {'0C'} {'10'}
function outtable = multTable(b,R,C)
% outTable = multTable(B,R,C)
% Describe what this function does here. This is more
% important for standalone functions than local functions
% but i don't know your application, so ...
%
% B specifies the numeric base (scalar, integer >= 2)
% R specifies the row headers. (non-negative integer)
% May be given as a scalar or a vector in any orientation.
% If specified as a scalar it will be expanded as 1:R.
% C specifies the blah blah blah etc
%
% outTable is a cellchar array ... etc
% make sure inputs are integer-valued
% this is required for dec2base()
% you can choose to enforce the range of these inputs in other ways
% i'm just going to clamp the values
% you could choose to throw an error instead
R = max(round(R),0);
C = max(round(C),0);
b = max(round(b),2);
% maybe you want to expand scalar inputs?
if isscalar(R); R = 1:R; end
if isscalar(C); C = 1:C; end
% orient vector inputs
R = reshape(R,[],1);
C = reshape(C,1,[]);
% build table and attach headers
mtable = R * C;
mtable = [R mtable];
mtable = [b C; mtable];
% convert the entire table and headers to base-b
outtable = num2cell(dec2base(mtable,b),2);
outtable = reshape(outtable,size(mtable));
% ...except the corner element should still be base-10
outtable{1,1} = sprintf('%d',b);
end
  2 件のコメント
Tom Schneider
Tom Schneider 2022 年 8 月 10 日
Thank you so much for the help, I gotta help my gf with her uni work and I usually code in python, so matlab hasnt been fun xD
I know about the base >10 issue, however this program should only be usable for 2<b<10, so thats not a problem.
I think I´ll use the "expant scalar inputs" i couldnt really think of any other solution besides num2str and then checking the string for ":" so thank you very much ^^
DGM
DGM 2022 年 8 月 10 日
It would otherwise work for scalar inputs, but your table would only have 1 row (or column), so I kind of figured that it'd be unlikely that anyone would actually want that. Allowing implicit expansion from a scalar allows for just a little convenience in what might be a common usage.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 8 月 10 日
If you change your code so as to no longer use table as a variable name (since table already has a meaning in MATLAB) you could build a multiplication table and store it as a table array.
x = 1:5;
n = numel(x);
t = table('Size', [n, n], ... % Preallocate t to be the correct size
'VariableTypes', repmat("string", 1, n), ... % All the elements will be strings
'RowNames', string(x), ... % Tables can have named rows
'VariableNames', string(x)) % and named variables (columns)
t = 5×5 table
1 2 3 4 5 _________ _________ _________ _________ _________ 1 <missing> <missing> <missing> <missing> <missing> 2 <missing> <missing> <missing> <missing> <missing> 3 <missing> <missing> <missing> <missing> <missing> 4 <missing> <missing> <missing> <missing> <missing> 5 <missing> <missing> <missing> <missing> <missing>
I'll fill in one element of the table.
t{4, 3} = 4*3
t = 5×5 table
1 2 3 4 5 _________ _________ _________ _________ _________ 1 <missing> <missing> <missing> <missing> <missing> 2 <missing> <missing> <missing> <missing> <missing> 3 <missing> <missing> <missing> <missing> <missing> 4 <missing> <missing> "12" <missing> <missing> 5 <missing> <missing> <missing> <missing> <missing>
Let's extract that element back out.
twelve = t{"4", "3"}
twelve = "12"
And to show you the elements in the table can be different sizes:
t{5, 3} = string(dec2bin(5*3)) + "_b"
t = 5×5 table
1 2 3 4 5 _________ _________ _________ _________ _________ 1 <missing> <missing> <missing> <missing> <missing> 2 <missing> <missing> <missing> <missing> <missing> 3 <missing> <missing> <missing> <missing> <missing> 4 <missing> <missing> "12" <missing> <missing> 5 <missing> <missing> "1111_b" <missing> <missing>
  2 件のコメント
DGM
DGM 2022 年 8 月 11 日
I was kind of hoping someone would take this approach. It would improve readability over a cellchar. I just never use tables, so I'm reluctant to provide my own novice examples thereof. :)
Steven Lord
Steven Lord 2022 年 8 月 11 日
One of the key points to remember when working with tables is how to store and access data inside them, and a key rule of thumb is that parentheses assign to or extract a sub-table while curly braces assign to or extract the contents of the sub-table. Those same rules of thumb apply to cell arrays: sub-cells (), contents {}.
t = array2table(magic(4))
t = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
subtable = t(1:2, 3:4)
subtable = 2×2 table
Var3 Var4 ____ ____ 3 13 10 8
numericArray = t{1:2, 3:4}
numericArray = 2×2
3 13 10 8
t(3:4, 1:2) = subtable
t = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 3 13 6 12 10 8 15 1
t{3:4, 3:4} = [999, 42; -1, Inf]
t = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 3 13 999 42 10 8 -1 Inf

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

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by