フィルターのクリア

Hello, is there any way I can define a matrix NxN whose elements are different real numbers between 0 and 5?

1 回表示 (過去 30 日間)
Hello, is there any way I can define a matrix NxN whose elements are different real numbers between 0 and 5?

回答 (4 件)

Taufik Sutanto
Taufik Sutanto 2013 年 10 月 13 日
Simply
rand(N)*5
will do ...
  2 件のコメント
jimaras
jimaras 2013 年 10 月 13 日
what about if I want to create a NxN matrix (1<N<2002) with random real number between 1 and 5 and the columns 1 and 2002 and the rows 1 and 2002 to be 0 ?

サインインしてコメントする。


Youssef  Khmou
Youssef Khmou 2013 年 10 月 13 日
try :
N=300;
M=5*rand(N);
  2 件のコメント
jimaras
jimaras 2013 年 10 月 13 日
what about if I want to create a NxN matrix (1<N<2002) with random real number between 1 and 5 and the columns 1 and 2002 and the rows 1 and 2002 to be 0 ?

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 13 日
N=5
out=reshape(linspace(0,5,N*N),N,N)
  2 件のコメント
jimaras
jimaras 2013 年 10 月 13 日
what about if I want to create a NxN matrix (1<N<2002) with random real number between 0 and 5 and the columns 1 and 2002 and the rows 1 and 2002 to be 0 ?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 13 日
編集済み: Azzi Abdelmalek 2013 年 10 月 13 日
M=rand(N)*5
M(randi(N*N))=0
M(randi(N*N))=5

サインインしてコメントする。


Image Analyst
Image Analyst 2013 年 10 月 13 日
In your comments, you specified both 0 and 1 as the lowest value, so I made it flexible enough to handle either one:
N = 10; % Whatever you want
lowestValue = 1; % or 0 - whichever you want.
highestValue = 5;
theMatrix = lowestValue + (highestValue - lowestValue) * rand(N)
% Zero out first and last rows.
theMatrix(1,:) = 0;
theMatrix(end,:) = 0;
% Zero out first and last columns.
theMatrix(:,1) = 0;
theMatrix(:,end) = 0;
  4 件のコメント
jimaras
jimaras 2013 年 10 月 13 日
I want to create two NxN matrices whose values can be between 0 and 5. I want later to implement these matrices in a prey & predator model so I think it's important to have values of 0 and of 5.
Image Analyst
Image Analyst 2013 年 10 月 13 日
編集済み: Image Analyst 2013 年 10 月 13 日
Like I said in the code, just change lowestValue:
lowestValue = 0;
What about the requirement that the outer edges of the array be zero? Recall where you said "the columns 1 and 2002 and the rows 1 and 2002 to be 0 " - to me, that means that column 1 and column 2002 (or the last column if N is different that 2002) should equal exactly 0. And the same for the first and last row - they are all zero. Again, is this still required? If not, just don't do the zeroing out and do:
N = 2002; % Whatever you want
lowestValue = 0; % or 1 - whichever you want.
highestValue = 5;
theMatrix = lowestValue + (highestValue - lowestValue) * rand(N)
Essentially (in an inflexible, hard coded manner):
theMatrix = 5 * rand(2002)
Did you follow what I said in my prior comment about never being able to hit any particular number exactly ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by