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:
Reordering the top half of a Matrix

Subject: Reordering the top half of a Matrix

From: Tania

Date: 13 Nov, 2012 03:24:11

Message: 1 of 3

I need to reorder a matrix randomly, but only the first upper half (as would be the result of triu(A,1) ).

Originally I tried randperm(numberofelements), but this doesn't work because I need only this top half changed.

Ideas?

Subject: Reordering the top half of a Matrix

From: Roger Stafford

Date: 13 Nov, 2012 05:52:14

Message: 2 of 3

"Tania" wrote in message <k7segr$3d9$1@newscl01ah.mathworks.com>...
> I need to reorder a matrix randomly, but only the first upper half (as would be the result of triu(A,1) ).
- - - - - - - - - -
 [m,n] = size(A);
 t = triu(reshape(1:m*n,m,n),1);
 p = t(t>0);
 q = p(randperm((2*n-min(m,n)-1)*min(m,n)/2));
 A(p) = A(q);

Roger Stafford

Subject: Reordering the top half of a Matrix

From: Roger Stafford

Date: 13 Nov, 2012 07:24:16

Message: 3 of 3

"Roger Stafford" wrote in message <k7sn6e$185$1@newscl01ah.mathworks.com>...
> [m,n] = size(A);
> t = triu(reshape(1:m*n,m,n),1);
> p = t(t>0);
> q = p(randperm((2*n-min(m,n)-1)*min(m,n)/2));
> A(p) = A(q);
>
> Roger Stafford
- - - - - - - - - -
  A slightly simpler version:

 t = triu(reshape(1:numel(A),size(A)),1);
 p = t(t>0);
 A(p) = A(p(randperm(numel(p))));

Roger Stafford

Tags for 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