I wonder how I can make an array (200,400) with values 0 to 7 which randomly distributed? I apperciate your help.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 30 Jun. 2011

0 Stimmen

rand(200,400)*7

2 Kommentare

Hassan
Hassan am 30 Jun. 2011
thanks Andrei. I wonder how I can group each value in the array. for eaxmple (1:20,1:20)=1,(40:70,40:70)=2 and so on.
Hassan
Hassan am 30 Jun. 2011
thanks but can you please show it with an example?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sean de Wolski
Sean de Wolski am 30 Jun. 2011

0 Stimmen

szs = [20 20 30]; %sizes
vals = [1 0 2]; %values
C = cellfun(@(c,v)ones(c)*v,num2cell(szs),num2cell(vals),'uni',false);
M = blkdiag(C{:});

3 Kommentare

Andrei Bobrov
Andrei Bobrov am 30 Jun. 2011
like
C = arrayfun(@(i1)ones(szs(i1))*vals(i1),1:3,'un',0);
Sean de Wolski
Sean de Wolski am 30 Jun. 2011
Nicer!
I don't think I've ever used arrayfun()
Hassan
Hassan am 30 Jun. 2011
thanks a lot Sean and Andrei.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by