# problems with creating a multiplication table

18 views (last 30 days)

Show older comments

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

##### 0 Comments

### Accepted Answer

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)

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
on 10 Aug 2022

### More Answers (1)

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)

I'll fill in one element of the table.

t{4, 3} = 4*3

Let's extract that element back out.

twelve = t{"4", "3"}

And to show you the elements in the table can be different sizes:

t{5, 3} = string(dec2bin(5*3)) + "_b"

##### 2 Comments

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))

subtable = t(1:2, 3:4)

numericArray = t{1:2, 3:4}

t(3:4, 1:2) = subtable

t{3:4, 3:4} = [999, 42; -1, Inf]

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!