-2

I want a matrix all filled with complex numbers and it would be better if it's a square matrix.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    Writing an incomplete question just to post a self-answer is discouraged. Your question must contain enough detail as if you _didn't_ know the answer when you posted it. [Ask] – Pranav Hosangadi May 31 '22 at 14:11
  • Please also read the descriptions of tags before you use them. _"Don’t use both the [tag:matlab] and [tag:octave] tags, unless the question is explicitly about the similarities or differences between the two"_ – Pranav Hosangadi May 31 '22 at 14:12

1 Answers1

-1

In order to generate random complex numbers in a matrix, you can do

A = (randi(3,3)) + (randi(3,3))*i*(randi(4))


A = (rand(3,3)) + (rand(3,3))*i*(rand(4))

rand would give real numbers whereas randi would only spew out integer values.

Here I have chosen a 3*3 matrix and imaginary part's magnitude will always be within 4.

  • 1
    Thanks for the self answer, but this is an extremely simple case, and one that the documentation would just answer. Not super relevant Q&A. – Ander Biguri May 31 '22 at 14:07
  • nvm I was just trying to help others. I didn't think it would waste your time. I googled for it and it didn't show up so I thought why don't I make it easier for the people who will google it later and just write an answer. But nvm... – Prajwal Sagar Jun 01 '22 at 05:04
  • Just to clarify: I did not downvote your post. However "random complex number matlab" shows the answer. Arguably I'd say a better answer, as `randi` are _integer valued_ complex numbers, you should do this with `rand` – Ander Biguri Jun 01 '22 at 08:49
  • 1
    Note that in `randi(3,3)` and `rand(3,3)`, the first `3` has a different meaning: for `randi` it is the range of output values, for `rand` it is the size of the first matrix dimension (number of rows). Both end up creating a 3x3 matrix, because if you give only one size it is used for both dimensions. But in the case of `randi(4)` (a scalar integer between 1 and 4) and `rand(4)` (a 4x4 matrix), the difference is important. In fact, the code you just added does not run, because you cannot multiply a 3x3 matrix by a 4x4 matrix. – Cris Luengo Jun 01 '22 at 20:25