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

In summary, the difference between * and .* is that .* performs an element-by-element multiplication.
  • #1
Ascendant78
328
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
  • #2
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 Ascendant78
  • #3
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?
 
  • #5
Oh, now I get it. Thank you so much for the information. I should be good now.
 
  • #6
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 Ascendant78

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

What is the difference between a script and a function in Matlab?

A script in Matlab is a file that contains a series of commands to be executed in sequential order. It is typically used for data analysis and visualization. A function, on the other hand, is a separate file that performs a specific task and can be called from other scripts or functions. Functions are useful for creating reusable code and streamlining repetitive tasks.

How can I troubleshoot errors in my Matlab code?

The first step in troubleshooting errors in Matlab is to carefully read the error message. It will often provide clues as to what went wrong. It is also helpful to use the debugging tools in Matlab, such as setting breakpoints and stepping through the code line by line, to identify the source of the error.

Why am I receiving an "Out of Memory" error in Matlab?

This error occurs when the amount of memory required to run the code exceeds the available memory on your computer. To fix this issue, try freeing up memory by closing other programs or reducing the size of your data. Alternatively, you can increase the amount of memory allocated to Matlab in the Preferences menu.

What is the difference between a cell array and a regular array in Matlab?

A regular array in Matlab can only hold numerical data of the same data type. A cell array, on the other hand, can hold data of different types, such as strings, numbers, and even other arrays. Cell arrays are useful for storing and manipulating heterogeneous data.

How can I import data from an external file into Matlab?

There are several ways to import data from an external file into Matlab, depending on the format of the data. For text files, you can use the readtable function. For Excel files, you can use the xlsread function. For other formats, such as CSV or JSON, there are specific functions or libraries available for importing the data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
979
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Precalculus Mathematics Homework Help
2
Replies
69
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
516
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top