フィルターのクリア

Matrix printing with unique values

1 回表示 (過去 30 日間)
Muhammad Bilal Qureshi
Muhammad Bilal Qureshi 2015 年 10 月 18 日
コメント済み: Walter Roberson 2015 年 10 月 19 日
Hi, I want to print a (4x3) matrix with random values between 1 and 12. I have done but the problem is that first, I want to print all 12 values (no missing value between 1 and 12). The second problem is that no value in the matrix cells should be repeated.
  2 件のコメント
Martin Schätz
Martin Schätz 2015 年 10 月 18 日
Hi, can you post your script so I know what you are actualy doing?
Muhammad Bilal Qureshi
Muhammad Bilal Qureshi 2015 年 10 月 18 日
編集済み: Walter Roberson 2015 年 10 月 19 日
dataresource_files = zeros(4,3);
for i=1:4
dataresource_files(i,1) = unique(fix(12*rand+1));
dataresource_files(i,1) = randi([1 12]);
dataresource_files(i,2) = randi([1 12]);
dataresource_files(i,3) = randi([1 12]);
end
dataresource_files

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

回答 (1 件)

Martin Schätz
Martin Schätz 2015 年 10 月 18 日
編集済み: Martin Schätz 2015 年 10 月 18 日
If you post some code I can elaborate and work with it, but like this I can only sugest to create vector from 1 to 12, permute it and reshape it to 4x3 matrix.
So the code would be like this:
dataresource_files = randperm(12);
dataresource_files = reshape(dataresource_files,[4 3]);
  3 件のコメント
Martin Schätz
Martin Schätz 2015 年 10 月 18 日
Hi again, with command randperm(n) your able to create random permutation where n = length of result and also ending number of permutation. So your code can be simplified like this:
dataresource_files = randperm(12);
dataresource_files = reshape(dataresource_files,[4 3]);
Each time you will run this code you will get new random order of numbers from 1 to 12 in array of 4x3 size.
Walter Roberson
Walter Roberson 2015 年 10 月 19 日
Right, and more compactly,
dataresource_files = reshape(randperm(12),[4 3]);

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by