MATLAB How to Create a Multiplication Matrix in MATLAB?

AI Thread Summary
To create a multiplication matrix in MATLAB, start by generating a vector from 0 to 1 with increments of 0.0101 using the colon operator. The first row and column of the matrix will be filled with this vector, and the remaining entries can be computed by multiplying each element in the column by the corresponding element in the row. A suggested code structure involves initializing a zero matrix and using nested loops to fill in the values, but vectorization can optimize this process. Additionally, ensure proper matrix dimensions when assigning values to avoid dimension mismatch errors. Understanding MATLAB's indexing and matrix operations is crucial for successfully implementing this task.
twotaileddemon
Messages
258
Reaction score
0
Hi, I'm new to MATLAB and I have a problem:

I am trying to create a multiplication matrix of sorts.

The first row will have numbers from 0 to 1 in steps of .0101
The first column will have the same, from 0 to 1 in steps of .0101
Then to generate the rest I will multiple each entry in the column by their corresponding row.
For example, the 2nd column, 2nd row entry will be .0101 * .0101
It will look like this:
0 .0101 .0202 ... 1
.0101 (.0101 * .0101) (.0101 * .0202) ... 1
.
.
.
1 ( 1 * .0101) (1 * .0202) ... 1

The problem is, I don't know how to write a code to do this. I'm sure it involves a loop of some sort with indexing from 1 to 100. Can someone help me?

I thought this might help somewhere:
indexfinal=99;
0:1/indexfinal:1;
Indeed, it gives me the steps of .0101 I'm looking for.
I just don't know how to make it so that I can create a matrix like the aboveformentioned.
 
Last edited:
Physics news on Phys.org
The great thing about MATLAB is that you can use vectorization (operations on vectors, or 1-D arrays) to speed things up. It doesn't do anything in parallel (I think) but it should go faster than if you had to loop everything using a double loop.

Do you know how to use the colon operator to extract / work with an entire row / column? If you had some m x n matrix (m rows by n columns) named foo, you could extract just the 2nd row (to, say, vector bar) by typing in the following:
>> bar = foo(2, :)

Or if you wanted the 3rd column, you'd type in:
>> bar = foo(:, 3)

You can also mix and match and select just the subset of rows 2-5, and columns 3-7:
>> bar = foo(2:5, 3:7)

Now, here's most of the way to the code you're looking for (comments follow the '%' symbol, effective until you start a new line):

Code:
% Row / column headings
indexfinal=99;
headings=0:1/indexfinal:1;

% Generate each row by multiplying the first row by the first column
subTable=zeros(indexfinal+1);
magicTable(1,:)=headings;	%first row
for n=2:indexfinal+1
	magicTable(n, :)=headings * headings(n);
end

% Fix first column

Fixing the first column is left as an exercise to the reader. You should probably also read the Mathworks' page on indexing:
http://www.mathworks.com/company/newsletters/articles/Matrix-Indexing-in-MATLAB/matrix.html

As well as the MATLAB documentation page on looping (mostly 'for' and 'while') and conditionality (mostly 'if'):
http://www.mathworks.com/help/techdoc/learn_matlab/f4-1931.html

As you can see, very comprehensive documentation (along with examples) is available at the Mathworks' website.

EDIT: You may need to make use of the transpose operator (the apostrophe symbol). It's here under list of arithmetic operations:
http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html
 
Last edited by a moderator:
I tried doing something like

indexfinal=99;
span = 0:1/indexfinal:1;
row(2,:) = span;
column(:,2) = span;

for n=2:indexfinal+1;
for m=2:indexfinal+1;
row(n,:) = span*span(n);
column(:,m) = span*span(m);
table = row * column;
end
end

But it still doesn't give me the desired result, and doesn't even work at all...
Right now I'm getting an error saying column(:,2) = span; has an assignment dimension mismatch, but I don't see why.

And this isn't even half of what I'm really doing. The reason I need the table the way it is is because I'm modeling the diffusion of water and I'm trying to use the table to represent values of C for 0 < C < 1 where I have to solve something like...
C(i,t) = dt*D(i)*[(C(i+1,t) + C(i-1,t) - 2*C(i,t))/(dx*dx)];
I'm supposed to take dt = 0.1 and dx = 1. D is just an array where every element is 0.001. Then I have to solve the equation somehow for values of C between 0 and 1. I think i and t act as some kind of index while the equation represents an approximation with derivatives.

Ideally, C is close to 0 near the center of the table, i.e. row 50 column 50, or around there, and closer to 1 at the far ends of the table.

I've been having such a hard time with this, I just can't seem to understand MATLAB at all...
 
Last edited:
twotaileddemon said:
I tried doing something like

indexfinal=99;
span = 0:1/indexfinal:1;
row(2,:) = span;
column(:,2) = span;

for n=2:indexfinal+1;
for m=2:indexfinal+1;
row(n,:) = span*span(n);
column(:,m) = span*span(m);
table = row * column;
end
end

But it still doesn't give me the desired result, and doesn't even work at all...
Right now I'm getting an error saying column(:,2) = span; has an assignment dimension mismatch, but I don't see why.

And this isn't even half of what I'm really doing. The reason I need the table the way it is is because I'm modeling the diffusion of water and I'm trying to use the table to represent values of C for 0 < C < 1 where I have to solve something like...
C(i,t) = dt*D(i)*[(C(i+1,t) + C(i-1,t) - 2*C(i,t))/(dx*dx)];
I'm supposed to take dt = 0.1 and dx = 1. D is just an array where every element is 0.001. Then I have to solve the equation somehow for values of C between 0 and 1. I think i and t act as some kind of index while the equation represents an approximation with derivatives.

Ideally, C is close to 0 near the center of the table, i.e. row 50 column 50, or around there, and closer to 1 at the far ends of the table.

I've been having such a hard time with this, I just can't seem to understand MATLAB at all...

There are two issues with your first few lines of code (where you create and try to fill row and column).

Firstly, you can't just invoke a matrix and then have partially filled or zero-padded values. You also can't resize these non-filled matrices (as you implicitly attempt to do). To make what you've got work (I think), you need to first do something like:
Code:
row=zeros(2, prod(size(span));

This creates a matrix named row that's filled with zeros, and has 2 rows by however big span happens to be. The reason I take the product of the result of size is that regardless of whether it's a row or column vector, the product will always be the number of elements in the vector.

Now, there may be a logical error in your algorithm in that the second command would (with the above) fill the second row of matrix row with the vector span. I call it a vector, but it should really be called a 1 x whatever matrix.
Code:
row(2,:) = span;

This brings me to the second problem, the reason you're getting the span error. span is a 1 x whatever matrix. You're attempting to fill a column (size: whatever x 1) with a row (size: 1 x whatever). So in order to make this work, you need to use the transpose (the apostrophe operator, ') of span:
Code:
column(:,2) = span';

This all said, MATLAB like any coding language, can be crashed and banged very to produce something that's functional. I don't mean to patronize, but it sounds like this may be what you're trying to do since you don't have some of the basics (e.g. indexing) in place. The following tutorials (video or written--I prefer the written myself) will help fill in some of the blank spots:
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

Before MATLABdude was MATLABdud, so you may have to follow the following algorithm:
1) code something
2) have it fail, bang head against wall / keyboard
3) look up the documentation via help <command> and through the documentation site
4) goto 1)

Good luck!

EDIT: Huh, having re-read my first post, it seems as if I already suggested the tutorials / documentation--I confused your post with another one that I answered at the same time, but where I'd forgotten to put in the tutorial suggestion!
 
Last edited:
Back
Top