DSPRelated.com
Forums

Comparing Array Elements of 2 x Different size Arrays

Started by fahad_imam February 25, 2010
Hello,

I am a newbie and need help to sort my experimental data in matlab, I got 2 x arryas of different size

Array A has values for time 0 till 20 in steps of 0.005
Array B has values for time 0 till 20 in steps of 0.02

0.005 0 0.02 0
0.01 0 0.04 -0.035137518
0.015 -0.002606174 0.06 -0.109273331
0.02 -0.00986774 0.08 -0.113182424
0.025 -0.020962419 0.1 -0.157273328
0.03 -0.035070996 0.12 -0.19430221
0.035 -0.051516113 0.14 -0.224681423
0.04 -0.069678694 0.16 -0.249264962
0.045 -0.089036882 0.18 -0.268491468
0.05 -0.109134611 0.2 -0.283986291
0.055 -0.129603662 0.22 -0.296455986
0.06 -0.150050397 0.24 -0.305987793
How can I compare the value in column 2 at .04 with value in column 4 at .04 time interval. There is step difference of three in array indices. I need a script or command to compare the ith and (j+3)th value. I hope the question is clear and would get some help soon.

thanks for your time.
Seems like, these are two signals operating at different sampling rates, and
you need at to compare these signals at absolute time values (Correct me if
I am wrong).

Since, the ratio of Fs of two datasets is 4. Therefore, every 4th sample is
A(faster) can be compared with set B(slower).

Hope this answers it, please let us know about your application so as to
better understand your query.
A = [
0.005 0
0.01 0
0.015 -0.002606174
0.02 -0.00986774
0.025 -0.020962419
0.03 -0.035070996
0.035 -0.051516113
0.04 -0.069678694
0.045 -0.089036882
0.05 -0.109134611
0.055 -0.129603662
0.06 -0.150050397];

B = [
0.02 0
0.04 -0.035137518
0.06 -0.109273331
0.08 -0.113182424
0.1 -0.157273328
0.12 -0.19430221
0.14 -0.224681423
0.16 -0.249264962
0.18 -0.268491468
0.2 -0.283986291
0.22 -0.296455986
0.24 -0.305987793];

ratio = 0.02/0.005 ;

cmp_res = zeros(1,size(A,1)/ratio)

for i = 1:size(A,1)/ratio
cmp_res(i) = (A(i*4,2) == B(i,2));
end

On Wed, Feb 24, 2010 at 3:57 AM, fahad_imam wrote:

>
> Hello,
>
> I am a newbie and need help to sort my experimental data in matlab, I got 2
> x arryas of different size
>
> Array A has values for time 0 till 20 in steps of 0.005
> Array B has values for time 0 till 20 in steps of 0.02
>
> 0.005 0 0.02 0
> 0.01 0 0.04 -0.035137518
> 0.015 -0.002606174 0.06 -0.109273331
> 0.02 -0.00986774 0.08 -0.113182424
> 0.025 -0.020962419 0.1 -0.157273328
> 0.03 -0.035070996 0.12 -0.19430221
> 0.035 -0.051516113 0.14 -0.224681423
> 0.04 -0.069678694 0.16 -0.249264962
> 0.045 -0.089036882 0.18 -0.268491468
> 0.05 -0.109134611 0.2 -0.283986291
> 0.055 -0.129603662 0.22 -0.296455986
> 0.06 -0.150050397 0.24 -0.305987793
>
> How can I compare the value in column 2 at .04 with value in column 4 at
> .04 time interval. There is step difference of three in array indices. I
> need a script or command to compare the ith and (j+3)th value. I hope the
> question is clear and would get some help soon.
>
> thanks for your time.
>
>
>

--
Regards,
Omer Rajput
Hardware Design Engineer
Digitek Engineering
I guess you haven't ran this script as it returns errors. First of all, you
said that 'A & B are matrices' and then later on you treat them as a
structures 'C = [A.values];' and 'plot(B.time,Y)'.

I think it should have been:

A_values = A(:,2); % Assuming values are in the second column.

A_time = A(:,1); % Assuming time instants are in the first column.

B_values = B(:,2); % Assuming values are in the second column.

B_time = B(:,1); % Assuming time instants are in the first column.
tmp = ismember(A_time, B_time);

D = A_values(tmp);

Y = abs( D - B_values(1:size(D,1)) );
plot( B_time(1:size(D,1)), Y );

I think you can also try downsample function.

A_d = downsample(A,4,3)

A_d
0.0200 -0.0099
0.0400 -0.0697
0.0600 -0.1501

*
*
On Fri, Feb 26, 2010 at 1:56 AM, Fahad Imam wrote:

>
> Yes, the signals are operating at different sample, basically due to load
> on target hardware I have to change the resolution of signal to 20ms. but
> before doing that I want to simulate the data for 20ms and find the error in
> final ouput.
>
> If A is matrix with 0.005 and B is matrix with 0.02 sample, I did the easy
> way
>
> tmp = ismember(A(:),B(:));
>
> C = [A.values];
>
> D = C(tmp);
>
> Y = abs(D-B);
>
> plot(B.time,Y)
> Is this make sense.
>
> Thanks for your help.
> Regards
> On Thu, Feb 25, 2010 at 1:30 PM, Omer Rajput wrote:
>
>> Seems like, these are two signals operating at different sampling rates,
>> and you need at to compare these signals at absolute time values (Correct me
>> if I am wrong).
>>
>> Since, the ratio of Fs of two datasets is 4. Therefore, every 4th sample
>> is A(faster) can be compared with set B(slower).
>>
>> Hope this answers it, please let us know about your application so as to
>> better understand your query.
>> A = [
>> 0.005 0
>> 0.01 0
>> 0.015 -0.002606174
>> 0.02 -0.00986774
>> 0.025 -0.020962419
>> 0.03 -0.035070996
>> 0.035 -0.051516113
>> 0.04 -0.069678694
>> 0.045 -0.089036882
>> 0.05 -0.109134611
>> 0.055 -0.129603662
>> 0.06 -0.150050397];
>>
>> B = [
>> 0.02 0
>> 0.04 -0.035137518
>> 0.06 -0.109273331
>> 0.08 -0.113182424
>> 0.1 -0.157273328
>> 0.12 -0.19430221
>> 0.14 -0.224681423
>> 0.16 -0.249264962
>> 0.18 -0.268491468
>> 0.2 -0.283986291
>> 0.22 -0.296455986
>> 0.24 -0.305987793];
>>
>> ratio = 0.02/0.005 ;
>>
>> cmp_res = zeros(1,size(A,1)/ratio)
>>
>> for i = 1:size(A,1)/ratio
>> cmp_res(i) = (A(i*4,2) == B(i,2));
>> end
>> On Wed, Feb 24, 2010 at 3:57 AM, fahad_imam wrote:
>>
>>>
>>>
>>>
>>> Hello,
>>>
>>> I am a newbie and need help to sort my experimental data in matlab, I got
>>> 2 x arryas of different size
>>>
>>> Array A has values for time 0 till 20 in steps of 0.005
>>> Array B has values for time 0 till 20 in steps of 0.02
>>>
>>> 0.005 0 0.02 0
>>> 0.01 0 0.04 -0.035137518
>>> 0.015 -0.002606174 0.06 -0.109273331
>>> 0.02 -0.00986774 0.08 -0.113182424
>>> 0.025 -0.020962419 0.1 -0.157273328
>>> 0.03 -0.035070996 0.12 -0.19430221
>>> 0.035 -0.051516113 0.14 -0.224681423
>>> 0.04 -0.069678694 0.16 -0.249264962
>>> 0.045 -0.089036882 0.18 -0.268491468
>>> 0.05 -0.109134611 0.2 -0.283986291
>>> 0.055 -0.129603662 0.22 -0.296455986
>>> 0.06 -0.150050397 0.24 -0.305987793
>>>
>>> How can I compare the value in column 2 at .04 with value in column 4 at
>>> .04 time interval. There is step difference of three in array indices. I
>>> need a script or command to compare the ith and (j+3)th value. I hope the
>>> question is clear and would get some help soon.
>>>
>>> thanks for your time.
>>>
>>>
>>> --
>> Regards,
>> Omer Rajput
>> Hardware Design Engineer
>> Digitek Engineering
>> --
> Thanks & Best Regards,
> Imam
>

--
Regards,
Omer
Why would you expect an error from down-sampling the signal ?, if your
sampling rate(50Hz)
is above the Nyquist-frequency there shouldn't be any problems.

In other words i would expect D to be zero, however from your matrix
print, I see D is not zero. Thus I it seems your samples is not
synchronised, i.e your 50HZ and 200Hz sampling didn't start at the same
time. However from the information you have supplied this is not an error,
but your method of validating your samples (abs(D-B)) is invalid.

your A and B data, did you simulate them, or are they from actual
measurements ?

Regards

On Thu, 25 Feb 2010 22:00:37 +0100, Fahad Imam wrote:

> The signals are operating at different sample, basically due to load on
> target hardware I have to change the resolution of signal to 20ms from
> 5ms.
> but before doing that I want to simulate the data for 20ms and find the
> error in final ouput.
>
> If A is matrix with 0.005 and B is matrix with 0.02 sample, I did the
> easy
> way
>
> tmp = ismember(A(:),B(:)); % Indicesof A which are common to B
> C = [A.values];
> D = C(tmp); % Values of A which are common to B
> Y = abs(D-B); % error in two data sets
> plot(B.time,Y)
>
> Is this make sense??
>
> Thanks for your help.
> Regards
>
> On Thu, Feb 25, 2010 at 1:58 PM, wrote:
>
>> Hi
>>
>> Your text suggest time starting at 0, and your printed matrix's starts
>> at
>> 0.005 at 0.02 respectively.
>>
>> Assuming you intend to start at t=0, the correct index conversion would
>> be
>> Aindex=(Bindex-1)*4+1, otherwise the conversion would be Aindex=Bindex*4
>> because 0.02/0.005=4
>> (remember MATLAB indexing starts at 1 not 0 like in C, and etc.)
>>
>> here is an example converting a random index in B to A (time starts at
>> t=0)
>>
>> % setup arrays
>> A=0:0.005:20;
>> B=0:0.02:20;
>>
>> A=[A;rand(size(A))]';
>> B=[B;rand(size(B))]';
>>
>> %index
>> j=round(rand*(length(B)-1)+1); %index in range [1:length(B)] for B
>> matrix
>>
>> jA=(j-1)*4+1; %index in A matrix
>>
>> %time and data
>> A(jA,:)
>> B(j,:)
>>
>> %only data
>> A(jA,2)
>> B(j,2)
>>
>> hope this helps getting you started.
>>
>> On Tue, 23 Feb 2010 23:57:16 +0100, fahad_imam
>> wrote:
>>> Hello,
>>>
>>> I am a newbie and need help to sort my experimental data in matlab, I
>>> got
>>> 2 x arryas of different size
>>>
>>> Array A has values for time 0 till 20 in steps of 0.005
>>> Array B has values for time 0 till 20 in steps of 0.02
>>>
>>> 0.005 0 0.02 0
>>> 0.01 0 0.04 -0.035137518
>>> 0.015 -0.002606174 0.06 -0.109273331
>>> 0.02 -0.00986774 0.08 -0.113182424
>>> 0.025 -0.020962419 0.1 -0.157273328
>>> 0.03 -0.035070996 0.12 -0.19430221
>>> 0.035 -0.051516113 0.14 -0.224681423
>>> 0.04 -0.069678694 0.16 -0.249264962
>>> 0.045 -0.089036882 0.18 -0.268491468
>>> 0.05 -0.109134611 0.2 -0.283986291
>>> 0.055 -0.129603662 0.22 -0.296455986
>>> 0.06 -0.150050397 0.24 -0.305987793
>>>
>>>
>>> How can I compare the value in column 2 at .04 with value in column 4
>>> at
>>> .04 time interval. There is step difference of three in array indices.
>>> I
>>> need a script or command to compare the ith and (j+3)th value. I hope
>>> the
>>> question is clear and would get some help soon.
>>>
>>> thanks for your time.
>>>
>>>
>>>