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:
sorting data

Subject: sorting data

From: Mathew Thomas

Date: 22 Mar, 2012 16:52:12

Message: 1 of 8

Hello All,

I have 2 arrays:
Array 1. (1 X 100) array of 0s and 1s.
Array 2. (1 X 50) array of values.

In this example, there will be fifty 1's and fifty 0's in Array 1. I want to sort array 2 to the same size of array 1, such that the 1's in array 1 are replaced by the values in array 2.

I have been using loops to do after finding the locations where value is 1. For a very large array this is time consuming. What is a more efficient way to do this ?

Thanks in advance.

Mathew

 

Subject: sorting data

From: Doug Schwarz

Date: 22 Mar, 2012 17:22:12

Message: 2 of 8

On 3/22/2012 12:52 PM, Mathew Thomas wrote:
> Hello All,
>
> I have 2 arrays:
> Array 1. (1 X 100) array of 0s and 1s.
> Array 2. (1 X 50) array of values.
>
> In this example, there will be fifty 1's and fifty 0's in Array 1. I
> want to sort array 2 to the same size of array 1, such that the 1's in
> array 1 are replaced by the values in array 2.
>
> I have been using loops to do after finding the locations where value is
> 1. For a very large array this is time consuming. What is a more
> efficient way to do this ?
> Thanks in advance.
>
> Mathew

array1(logical(array1)) = array2;


--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: sorting data

From: Roger Stafford

Date: 22 Mar, 2012 17:25:17

Message: 3 of 8

"Mathew Thomas" wrote in message <jkflbs$miv$1@newscl01ah.mathworks.com>...
> Hello All,
>
> I have 2 arrays:
> Array 1. (1 X 100) array of 0s and 1s.
> Array 2. (1 X 50) array of values.
>
> In this example, there will be fifty 1's and fifty 0's in Array 1. I want to sort array 2 to the same size of array 1, such that the 1's in array 1 are replaced by the values in array 2.
>
> I have been using loops to do after finding the locations where value is 1. For a very large array this is time consuming. What is a more efficient way to do this ?
>
> Thanks in advance.
>
> Mathew
- - - - - - -
 array3 = zeros(1,100);
 array3(array1~=0) = array2;

  (I think this works. If not, use array3(find(array1~=0)) = array2 .)

Roger Stafford

Subject: sorting data

From: Mathew Thomas

Date: 22 Mar, 2012 18:09:25

Message: 4 of 8

Thanks Roger and Doug.

@Roger: It did work without the 'find' function.

-Mathew

Subject: sorting data

From: Mathew Thomas

Date: 24 Mar, 2012 00:26:31

Message: 5 of 8

"Roger Stafford" wrote in message <jkfn9t$t58$1@newscl01ah.mathworks.com>...
> "Mathew Thomas" wrote in message <jkflbs$miv$1@newscl01ah.mathworks.com>...
> > Hello All,
> >
> > I have 2 arrays:
> > Array 1. (1 X 100) array of 0s and 1s.
> > Array 2. (1 X 50) array of values.


Now if array 1 is the same (1X100) and array 2 is (50X50), then how do I use ismember function to get a matrix of size 100 X 50.


a = rand(1,100);
b = rand(50,50);
common = ismember(a,b);

I get a 1X100 matrix if I do this as expected, but I need a 100 X 50 matrix. Can I do this without using a loop for each column of the matrix or using repmat to make the variable 'a' have same number of columns as b ?

Thanks in advance,

-Mathew
 

Subject: sorting data

From: Roger Stafford

Date: 24 Mar, 2012 02:25:38

Message: 6 of 8

"Mathew Thomas" wrote in message <jkj4bn$h79$1@newscl01ah.mathworks.com>...
> Now if array 1 is the same (1X100) and array 2 is (50X50), then how do I use ismember function to get a matrix of size 100 X 50.
>
> a = rand(1,100);
> b = rand(50,50);
> common = ismember(a,b);
> .......
- - - - - - - -
  You need to explain your revised problem more clearly. You want a 100-by-50 result, but what are each of the 100 rows of it to contain and how is it related to the contents of a and b? If you write "ismember(a,b)" you are apparently looking for elements in 'a' which are contained in 'b', but how does that give a 100-by-50 result and what are these results?

  I suggest you temporarily cut down your sizes to 5 and 10 (or smaller) rather than 50 and 100 and give us a concrete example, (with specific numbers - don't use 'rand'), of what you would expect from it.

  Note that in your code

 a = rand(1,100);
 b = rand(50,50);

the odds are astronomically against finding any of the elements in 'a' also present in 'b'. The 'rand' function values can be any of about 4.5e15 possibilities and that is an enormous number - four and a half million billion.

Roger Stafford

Subject: sorting data

From: Mathew Thomas

Date: 24 Mar, 2012 04:59:12

Message: 7 of 8

A is a matrix of frequency values. B = unique(A)
A = B =
[ [
210.24 123.54 200.00 123.54
240.65 240.65 230.35 200.00
] 210.65
                                                                                      230.35
                                                                                      240.65
                                                                                   ]
I need A to be the same size of B for each column. I need to get something like this:
ismember(B,A) =
[
0 1 0
0 0 1
0 0 0
0 0 1
0 1 0
]

i.e., 123.54 is the first value in B, so '1' is the first value in second column and 240.65 is the last value in B, so that is '1' as well.

Hope this gives a better idea.

Thanks,
Mathew

Subject: sorting data

From: Roger Stafford

Date: 24 Mar, 2012 05:59:12

Message: 8 of 8

"Mathew Thomas" wrote in message <jkjkb0$2n1$1@newscl01ah.mathworks.com>...
> A is a matrix of frequency values. B = unique(A)
> A = B =
> [ [
> 210.24 123.54 200.00 123.54
> 240.65 240.65 230.35 200.00
> ] 210.65
> 230.35
> 240.65
> ]
> I need A to be the same size of B for each column. I need to get something like this:
> ismember(B,A) =
> [
> 0 1 0
> 0 0 1
> 0 0 0
> 0 0 1
> 0 1 0
> ]
>
> i.e., 123.54 is the first value in B, so '1' is the first value in second column and 240.65 is the last value in B, so that is '1' as well.
>
> Hope this gives a better idea.
>
> Thanks,
> Mathew
- - - - - - - - -
  This problem is ideal for 'ismember' applied to A, one column at a time, and that is what you should do in this situation. For the k-th column of A use "ismember(B,A(:,k))" to get the k-th column of your result. What could be simpler?

  A solution without loops is very likely to be slower and use more memory in my opinion. Seeking "vectorized" solutions is something that definitely can be overdone.

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