Matlab - referencing a general element?

In summary, it is possible to associate each element of a matrix with its row and column number without explicitly doing each one in turn. This can be achieved by using the meshgrid function and applying it to the matrix and a function that takes in the row and column numbers as input values. This allows for the creation of a new matrix with the desired elements.
  • #1
dt19
47
0
Hi,

Is there a way to associate each element with its row and column number without explicitly doing each one in turn? I have a matrix and I want to multiply it by a matrix whose elements are a function which takes as its input values the row and column number of the first matrix.
(I think essentially I'm trying to treat the matrix as a grid of coordinates).
What I'm trying to do is to say,
new_matrix(a,b) = old_matrix(a,b) * f(a,b)
where a and b are the row and column number.

My problem is I don't know how to tell MATLAB that I want a to be the row number and b to be the column number. My current idea is to create two matrices, one with all the column values equal and incrementing by 1, and the other the same but with row values equal, and treat these as x and y matrices, but this seems like a rather long-winded way of doing it. Can anyone help? (I'm new to MATLAB so it's probably something really obvious!)
 
Physics news on Phys.org
  • #2
Yes, it is possible to do that with MATLAB. The most straightforward way is to use the meshgrid function, which takes two vectors and creates a coordinate grid from them. For example: [a,b] = meshgrid(1:size(old_matrix,1), 1:size(old_matrix,2))new_matrix = old_matrix .* f(a,b)The meshgrid function will create two matrices, a and b, with the appropriate row and column numbers. You can then use these to call your function f and apply it to the elements of old_matrix.I hope this helps!
 

1. What is the syntax for referencing a specific element in Matlab?

The syntax for referencing a specific element in Matlab is variableName(row, column), where variableName is the name of the matrix or array, row is the index of the row, and column is the index of the column.

2. How do I reference an element in a multidimensional array in Matlab?

To reference an element in a multidimensional array in Matlab, you can use multiple indices separated by commas. For example, variableName(row, column, depth) would reference an element in a three-dimensional array.

3. Can I use variables as indices when referencing elements in Matlab?

Yes, you can use variables as indices when referencing elements in Matlab. This can be useful when you want to loop through a matrix or array and access different elements each time.

4. What happens if I try to reference an element outside the bounds of a matrix in Matlab?

If you try to reference an element outside the bounds of a matrix in Matlab, you will get an error message. It is important to make sure your indices are within the size of the matrix to avoid these errors.

5. How do I reference the last element in a row or column in Matlab?

To reference the last element in a row or column in Matlab, you can use the end keyword. For example, variableName(end, 1) would reference the last element in the first column of a matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
865
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
837
  • Linear and Abstract Algebra
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Precalculus Mathematics Homework Help
Replies
32
Views
852
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
543
Back
Top