Another Matlab Matrix question

  • Context: MATLAB 
  • Thread starter Thread starter rt1870
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion revolves around optimizing MATLAB code for creating a specific type of matrix. The matrix should have zeros along the diagonal, with the remaining elements being either zero or random numbers between -1 and 1, distributed in a specified proportion. The focus is on finding a more efficient method than the current implementation, which relies on nested loops.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty with vectorization in MATLAB and seeks an efficient method for creating a matrix with specific properties.
  • Another participant suggests that not all operations in MATLAB can be vectorized, indicating a limitation in achieving the desired efficiency.
  • A participant mentions that their current method involves creating a vector with the correct proportion of random numbers and zeros, but finds the subsequent assignment to the matrix clumsy and inefficient.
  • One suggestion involves using the diag() command in combination with rand() to simplify the process of setting matrix elements.
  • A participant responds that they have attempted to use diag() but found it limited to setting or extracting diagonal elements, and struggles with setting non-diagonal elements without extensive looping.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single efficient method for creating the matrix. There are differing opinions on the capabilities of MATLAB's vectorization and the effectiveness of the diag() command for the task at hand.

Contextual Notes

The discussion highlights limitations in the current approach, including the reliance on loops and the challenges of achieving the desired matrix configuration efficiently. There is also an acknowledgment of the potential for vectorization to improve performance, though its applicability is debated.

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

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K