フィルターのクリア

problem of generations of values in a matrix

1 回表示 (過去 30 日間)
dakhli mohamed
dakhli mohamed 2018 年 10 月 27 日
コメント済み: dakhli mohamed 2018 年 10 月 27 日
Hi I have a matrix of 3 * 3 and I want to fill it by   4 value of 1,    3 value of 2   and 2 value 2 no matter the location I tested this code but I did not find the desired result.
function [matrice] = hyperImage(dim , n1 ,n2 ,n3 ) %% n1: number of 1 ,n2: number of 2,n3: number of 3
matrice=zeros(dim,dim); %%Fill a desired domention matrix with a value of zero
d=dim-1;
for i=1:n1
r=round(rand*d)+1;
c=round(rand*d)+1;
matrice(r,c)=1;
end
for i=1:n2
r=round(rand*d)+1;
c=round(rand*d)+1;
matrice(r,c)=2;
end
for i=1:n3
r=round(rand*d)+1;
c=round(rand*d)+1;
matrice(r,c)=3;
end

採用された回答

Guillaume
Guillaume 2018 年 10 月 27 日
The first thing your code should do is check if the inputs are compatible and return an throw an error message explaining the problem otherwise:
assert(n1 + n2 + n3 == dim^2, 'the count of elements must match the size of the matrix);
Or you could change the signature of your function so that it doesn't ask for dim since it's redundant (it's the square root of n1+n2+n3, and that sum is not a square, you throw an error).
Secondly, to generate random integers you shoul use randi(d) unstead of round(rand*d)+1.
The biggest problem with your code is that it doesn't ensure that the different coordinates you generate are all different, so it is extremely likely that you're going to fill the same spot more than once (and hence miss other spots).
The easiest way to do what you want:
function matrice = hyperImage(dim, n1 ,n2 ,n3)
assert(n1 + n2 + n3 == dim^2, 'the count of elements must match the size of the matrix);
filler = repelem([1 2 3], [n1 n2 n3]);
matrice = reshape(filler(randperm(numel(filler))), dim, dim);
end
  1 件のコメント
dakhli mohamed
dakhli mohamed 2018 年 10 月 27 日
thank you very much for you Guillaume :D merciiiiiiii

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

その他の回答 (2 件)

Stephen23
Stephen23 2018 年 10 月 27 日
編集済み: Stephen23 2018 年 10 月 27 日
>> V = [4,3,2]; % [#1s, #2s, #3s, etc.]
>> F = @(v,n)n*ones(1,v);
>> C = arrayfun(F,V,1:numel(V),'uni',0); % or use REPELEM
>> M = nan(sqrt(sum(V)));
>> M(randperm(sum(V))) = [C{:}]
M =
2 3 2
2 1 1
1 1 3
This works for any square matrix, i.e. the values in V must sum to 1, or 4, or 9, or 16, or 25, etc., but could easily be adapted for any matrix size.
  1 件のコメント
dakhli mohamed
dakhli mohamed 2018 年 10 月 27 日
thank you very much for you Stephen Cobeldick

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


John D'Errico
John D'Errico 2018 年 10 月 27 日
編集済み: John D'Errico 2018 年 10 月 27 日
No. You are thinking about it the wrong way.
m = [1 1 1 1, 2 2 2, 3 3]; You can use repelem for the general case
m = reshape(m(randperm(9)),3,3)
m =
2 3 2
1 2 3
1 1 1
So stuff a variable with the desired number of 1's, 2's, and 3's. Then just randomly permute the vector and reshape it as desired.
  1 件のコメント
dakhli mohamed
dakhli mohamed 2018 年 10 月 27 日
thank you very much for you John D'Errico :D

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

カテゴリ

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