Randi command for a matrix

5 ビュー (過去 30 日間)
Bilal Khurshid
Bilal Khurshid 2019 年 9 月 19 日
コメント済み: Walter Roberson 2019 年 9 月 20 日
i am using a random generator for a matrix.
However the randi command is not working for matrix C with following error (Error using randi First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or equal to IMAX.)
clear all
clc
A = (textread('data_mat_ta01_11x5.txt'))'
size(A)
B=A/9
C=floor(B)
D=randi(C)

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 19 日
編集済み: KALYAN ACHARJYA 2019 年 9 月 19 日
Wrong here,you read a txt file as A and Divide it by 9>>B, Is there any sense??
% Read a text file, variable name A
A = (textread('data_mat_ta01_11x5.txt'))'
size(A) % No use, you have not assigned to any variable
B=A/9
and
floor rounds each element of B to the nearest integer less than.
Regarding randi matrix generation: See the following example
%...........[3,4] reperesent the size of the required matrix
>> randi(10,[3,4]);
%........^Max element value in the matrix
ans =
9 10 3 10
10 7 6 2
2 1 10 10
Rquested you to debug the code, line by line and modify it accordingly. If you are just a beginner on Matlab, I suggested you to go through this
Good Wishes!
  2 件のコメント
Bilal Khurshid
Bilal Khurshid 2019 年 9 月 20 日
Dear Kalyan
Thanks for your response.
I have to make a code for random selection of all matrix values from as assumed interval [1,A/9]
A = (textread('data_mat_ta01_11x5.txt'))' %Reads file data_mat_ta01_11x5.txt and takes values for matrix A with size 11x5
size(A) % Verifies size of matrix A, to confirm it is 11x5
B=A/9 % divides all values of matrix A by 9 to meet the interval condition A/9
C=floor(B) % Converts values of matrix B to integer value as after division some values are decimal
D=randi(C) % Command to randomly select all values of the matrix in the range [1-A/9], however it is not working, is randi command ok for this purpose? or i have to use the range command
Walter Roberson
Walter Roberson 2019 年 9 月 20 日
You cannot pass an 11 x 5 array as the first parameter to randi().
D = arrayfun( @(c) randi([1 c]), C);

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

カテゴリ

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

製品


リリース

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by