MATLAB Another Matlab Matrix question

  • Thread starter Thread starter rt1870
  • Start date Start date
  • Tags Tags
    Matlab Matrix
AI Thread Summary
The discussion centers on optimizing MATLAB code to create a matrix with zeros along the diagonal and a specific proportion of random numbers between -1 and 1 in the non-diagonal positions. The user currently employs a method involving nested loops to assign values, which is deemed inefficient, especially since the matrix is generated tens of thousands of times in a Monte Carlo simulation. The user seeks a more efficient, vectorized approach to achieve this matrix configuration. While there is acknowledgment that not all tasks can be vectorized in MATLAB, suggestions include using the diag() function in combination with random number generation. However, the user has found that diag() primarily facilitates diagonal manipulation and does not assist in setting non-diagonal elements without extensive looping. The conversation highlights the need for improved efficiency in matrix operations within MATLAB, particularly for repetitive tasks in simulations.
rt1870
Messages
3
Reaction score
0
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
 
Physics news on Phys.org
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.
 

Similar threads

Back
Top