Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Point Sampling MAtrix in Matlab

Subject: Point Sampling MAtrix in Matlab

From: Alex

Date: 9 Feb, 2012 16:51:13

Message: 1 of 3

Dear all,
I would like to create in matlab a point sampling matrix of [m,n] dimension.


Such a matrix has m rows and n columns
-it has all the elements zeros but each row has one element "1" random
but
-each column can only have one "1" element

Is it possible in matlab to do something like that in a smart way? Of course one can build it with loops of for and while but this is gonna take ages to finish when the matrix size increases

Regards
Alex

Subject: Point Sampling MAtrix in Matlab

From: Roger Stafford

Date: 9 Feb, 2012 19:48:32

Message: 2 of 3

"Alex " <alaios@yahoo.com> wrote in message <jh0ti1$hvp$1@newscl01ah.mathworks.com>...
> Dear all,
> I would like to create in matlab a point sampling matrix of [m,n] dimension.
>
>
> Such a matrix has m rows and n columns
> -it has all the elements zeros but each row has one element "1" random
> but
> -each column can only have one "1" element
>
> Is it possible in matlab to do something like that in a smart way? Of course one can build it with loops of for and while but this is gonna take ages to finish when the matrix size increases
>
> Regards
> Alex
- - - - - - - - -
  With m rows each containing 1 one that would make a total of m ones altogether. With n columns each containing 1 one it would be a total of n ones. Therefore your request is impossible unless m and n are equal.

  With m equal to n, do this:

 x = zeros(n);
 x(randperm(n)+n*(0:n-1)) = 1;

Roger Stafford

Subject: Point Sampling MAtrix in Matlab

From: Roger Stafford

Date: 9 Feb, 2012 20:02:10

Message: 3 of 3

"Roger Stafford" wrote in message <jh17ug$odg$1@newscl01ah.mathworks.com>...
> x = zeros(n);
> x(randperm(n)+n*(0:n-1)) = 1;
- - - - - - - - -
  Another way is:

 x = eye(n);
 x = x(randperm(n),:); % Or x(:,randperm(n))

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us