# Another Matlab Matrix question

• MATLAB
• rt1870
In summary, the author is trying to speed up code they have written in MATLAB and is yet to get to grips with vectorization.
rt1870
I am trying to speed up some code I have written in MATLAB, and I yet to get to grips with the whole vectorization thing. Could somebody tell me an efficient way of doing the following:

I need to create a matrix with zeros along the diagonal and the other elements are either zero or a random number between -1 and 1, in a fixed proportion and randomly placed. e.g. a 5 by 5 matrix with zeros along the diagonal and 40% of the remaining 20 elements are zero and 60% of the remaining elements are between -1 and 1 (doesn't especially matter to me if zero is included in that). If the given proportion of the remaining non-diagonal elements is not an integer, then it should be rounded to the closest integer.

I have done this already by creating a vector with the correct proportion of zeros and numbers randomly placed, and then used two for loops and an if statement to assign these elements to the non diagonal positions in the matrix. This seems really inefficient and I am sure there is a nice simple way of doing what I want in MATLAB! I have scoured the documentation but can't figure it out.

Any help would be greatly appreciated

Welcome to PhysicsForums!

I don't think there's a single (vectorized) command that can do this all--vectorization is admittedly not my strong suit, but not everything in MATLAB can be vectorized.

Perhaps you are correct, but I have been consistently impressed with how Matlab handles matrices in the (short) time I have used it.

The program I am running is a Monte Carlo type experiment so I have create this matrix tens of thousands of times every run of my model, so any more efficient method than my own would help immensely. The documentation recommends vectorising as much as possible for better speed.

Basically my method is to create a vector with n(n-1) elements with the correct proportion of random numbers and zeros, which does not seem to resource intensive. My next bit of code feels really clumsy though:

A=zeros(n)
counter1=1
for counter2=1:n
for counter3=1:n
if counter2~=counter3
A(counter2,counter3)=vector_with_values(counter1)
counter1=counter1+1
end
end
end

Last edited:
have you tried the diag() command to help out? I would think rand() in combination with diag() could do most of what you want to do fairly easily.

Yes, I have attempted to use diag() in various ways, but I can only set what is on the diagonal with it, or extract the diagonal from a matrix. What I need to do is set the non-diagonal elements of the matrix but can't figure it out without lots of loops.

## What is a matrix in Matlab?

A matrix in Matlab is a two-dimensional array of numbers or values. It is a fundamental data structure used for storing and manipulating data in Matlab.

## How do I create a matrix in Matlab?

To create a matrix in Matlab, you can use the "matrix" function or the square brackets notation. For example, matrix = [1 2 3; 4 5 6; 7 8 9] will create a 3x3 matrix with the values 1 to 9.

## How do I perform operations on matrices in Matlab?

Matlab has built-in functions for performing operations on matrices, such as addition, subtraction, multiplication, and division. You can also use loops and conditional statements to perform more complex operations on matrices.

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

In Matlab, a matrix is a two-dimensional array with rows and columns, while an array can have any number of dimensions. Matrices are typically used for mathematical operations, while arrays can store any type of data.

## How do I access specific elements in a matrix in Matlab?

You can access specific elements in a matrix by using the row and column indices. For example, matrix(2,3) will return the element in the second row and third column of the matrix. You can also use a colon to access a range of elements, such as matrix(1:3,2) to access the first three elements in the second column.

• MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
463
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
901
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K