Why Does Multiplying the Identity Matrix by Random Values Affect All Elements?

  • Context: MATLAB 
  • Thread starter Thread starter Ascendant78
  • Start date Start date
  • Tags Tags
    Issues Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a diagonal matrix in MATLAB, specifically one that contains random numbers on the diagonal while all off-diagonal elements are zero. Participants explore different methods to achieve this, including the use of the identity matrix and the diag() function.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant expresses confusion about why their attempt to create a diagonal matrix using the identity matrix and random values results in non-zero elements outside the diagonal.
  • Another participant, unfamiliar with MATLAB, seeks clarification on the difference between matrix multiplication (*) and element-wise multiplication (.*), indicating a lack of resources for learning MATLAB.
  • A later reply clarifies that the * operator performs matrix multiplication, which does not achieve the desired effect when combined with the identity matrix, while the .* operator performs element-wise multiplication.
  • Another participant suggests using the diag() function as an alternative method to create a diagonal matrix with random integers or floats, providing examples of its usage.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method, as different approaches are discussed without resolving which is superior. The discussion remains open with multiple proposed solutions.

Contextual Notes

Some participants express uncertainty about MATLAB syntax and functionality, indicating potential gaps in their understanding of the language's operations.

Ascendant78
Messages
327
Reaction score
0
I need to figure out how to create a matrix that has zeros in everything except the diagonal, and I need the diagonal to be random numbers from 1 to 5. I tried a few different variations like this:

R = eye(5)*7*randi(10,5)

But I keep ending up with values in every row and column and I am at a loss as to why. I would think that multiplying the random by the identity matrix would clear out everything but the diagonal, but this code doesn't seem to be the right way to go about doing that. If anyone can help me out, I'd appreciate it.
 
Physics news on Phys.org
Ascendant78 said:
I need to figure out how to create a matrix that has zeros in everything except the diagonal, and I need the diagonal to be random numbers from 1 to 5. I tried a few different variations like this:

R = eye(5)*7*randi(10,5)

But I keep ending up with values in every row and column and I am at a loss as to why. I would think that multiplying the random by the identity matrix would clear out everything but the diagonal, but this code doesn't seem to be the right way to go about doing that. If anyone can help me out, I'd appreciate it.
Do you know the difference, in Matlab, between * and .* ?
 
  • Like
Likes   Reactions: Ascendant78
Regretfully not. The only language I am at all familiar with is some C++. Matlab is completely new to me. Our instructor threw a project at us with Matlab and told us the instructions guided us through all of it, so knowledge of Matlab was not necessary. That was not even remotely the case and I have spent hours on end trying to figure out the convoluted instructions.

It seems like finding this type of stuff out online is like finding a needle in a haystack. C++ resources have always been easy, but finding decent resources for Matlab just seems daunting.

I tried to do a web search on .* and had no luck finding it. Can you tell me what it does?
 
Oh, now I get it. Thank you so much for the information. I should be good now.
 
Another way to do this is using the diag() function, which can do two things

- Create a new diagonal matrix with the elements in the input vector on the main diagonal
- Return the diagonal elements from an existing matri (default is the main diagonal, otherwise you use a number as the second input to specify).

So for a 5x5 with random integers between 1 and 5 you could do,

Code:
diag(randi(5,5,1))

ans =

     5     0     0     0     0
     0     5     0     0     0
     0     0     1     0     0
     0     0     0     5     0
     0     0     0     0     4

Or for random floats,

Code:
r = 1 + 4*rand(5,1);
diag(r)
ans =

    1.3902         0         0         0         0
         0    2.1140         0         0         0
         0         0    3.1875         0         0
         0         0         0    4.8300         0
         0         0         0         0    4.8596

Hope this helps.
 
  • Like
Likes   Reactions: Ascendant78

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
3
Views
3K