How to Create a Multiplication Matrix in MATLAB?

In summary, the author is trying to create a multiplication matrix, but does not know how to write a code to do it. He finds help on the Mathworks website and finds that he needs to use the colon operator to extract / work with an entire row / column.
  • #1
twotaileddemon
260
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
  • #2
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:
  • #3
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:
  • #4
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:
  • #5


Hi there,

Thank you for reaching out for help with your MATLAB problem. I would be happy to assist you in creating a multiplication matrix using the information you have provided.

First, let's break down the steps you need to take to create this matrix:

1. Create a vector with values from 0 to 1 in steps of 0.0101. You can use the "linspace" function to do this. It takes in three inputs: the starting value, the ending value, and the number of points you want in between. In this case, it would look like this:
x = linspace(0, 1, 101); % creates a vector with 101 points from 0 to 1 in steps of 0.0101

2. Create a matrix with the same number of rows and columns as the vector you just created. You can use the "zeros" function to do this. It takes in two inputs: the number of rows and the number of columns. In this case, it would look like this:
A = zeros(101, 101); % creates a 101x101 matrix filled with zeros

3. Use a loop to fill in the matrix with the values you want. You can use the "for" loop to do this. It takes in three inputs: the index variable, the starting value for the index, and the ending value for the index. In this case, it would look like this:
for i = 1:101 % loops through all the rows
for j = 1:101 % loops through all the columns
A(i,j) = x(i) * x(j); % multiplies the values in the corresponding row and column
end
end

4. Print the matrix to see the results:
disp(A);

This should give you the multiplication matrix you are looking for. Of course, you can modify the code to fit your specific needs, such as changing the number of points or the step size.

I hope this helps you with your problem. If you have any further questions or need clarification, please don't hesitate to ask. Happy coding!
 

FAQ: How to Create a Multiplication Matrix in MATLAB?

1. What is the difference between a matrix and an array in Matlab?

A matrix is a two-dimensional data structure that contains rows and columns, while an array is a multi-dimensional data structure that can have any number of dimensions. In Matlab, a matrix is a special case of an array where the number of rows is equal to the number of columns.

2. How do I create a matrix or array in Matlab?

To create a matrix or array in Matlab, you can use the "zeros" or "ones" function to initialize a matrix or array of a specified size with all zeros or ones, respectively. You can also use the "eye" function to create an identity matrix. Additionally, you can use the "reshape" function to convert a vector into a matrix or array.

3. How do I access and manipulate elements in a matrix or array?

You can access and manipulate elements in a matrix or array by using their indices. For example, to access the element in the second row and third column of a matrix A, you would use A(2,3). You can also use slicing notation to access specific rows or columns or use logical indexing to access elements that meet certain conditions.

4. What are some common functions for working with matrices and arrays in Matlab?

Some common functions for working with matrices and arrays in Matlab include "size" to determine the size of a matrix or array, "transpose" to transpose a matrix or array, "diag" to extract or create a diagonal matrix, and "cat" to concatenate matrices or arrays along a specified dimension. You can also perform various mathematical operations such as addition, multiplication, and inversion on matrices and arrays in Matlab.

5. Can I perform matrix or array operations efficiently in Matlab?

Yes, Matlab is optimized for matrix and array operations, making it a powerful tool for scientific computing. It uses specialized algorithms and data structures to perform these operations efficiently, making it a popular choice for tasks such as data analysis, signal processing, and image processing.

Similar threads

Back
Top