problems with creating a multiplication table

20 views (last 30 days)
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

Accepted Answer

DGM
DGM on 10 Aug 2022
Edited: DGM on 10 Aug 2022
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 Comments
DGM
DGM on 10 Aug 2022
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.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 10 Aug 2022
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 Comments
Steven Lord
Steven Lord on 11 Aug 2022
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

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by