Comparing array elements of different lengths
3 views (last 30 days)
Show older comments
I'm trying to compare two arrays that are different lengths. One array will always be smaller than the other.
Array1 - stores pulses given
Array2 - stores neurons spiking information
I want to go through Array2 and match it the correct pulse in Array 1 and get the difference between the times (spike delay).
One way I've thought of is to have it go through Array2 and compare this value so that it falls in between two values in Array1.
So if Array2 has 2.6
Array1: .5 1.0 1.5 2.0 2.5 3.0
If would go through array 1 and then match between 2.5 and 3.0. Then it would subtract from the first value (2.5) to get the difference .1 and store in a different array.
For every time it doesn't match between numbers, I want it to count that which is the f_count. It would mean that the neuron did not spike.
true_spike = zeros(length(array2));
spike_delay = zeros(length(array2));
f_count = 0;
for i = 1:length(array2)
for j = 1:length(array1)
if array2(i,1) <= array1(j+1,1) && array2(i,1) > array1(j,1)
true_spike(i,1) = array2(i,1);
spike_delay(i,1) = array2(i,1) - array1(j,1);
else
f_count = f_count + 1;
end
end
end
spike_delay(spike_delay==0) = [];
spike_delay = spike_delay';
4 Comments
Ameer Hamza
on 7 Mar 2020
In your excel file, look at the cells D9 and D10. Is that a mistake? Both subtract B7 from A16 and A17. But B7 comes between A17 and A18, so shouldn't you only have A17-B7. Is this correct?
Answers (1)
Ameer Hamza
on 7 Mar 2020
The code gives the value according to the specified rule. The result matches with first few values in excel file
pulses = xlsread('Spiking_Example.xlsx', 'A:A');
neurons = xlsread('Spiking_Example.xlsx', 'B:B');
neurons(end) = []; % ignore last 0 in second column of excel file
difference = pulses - neurons';
mask = difference > 0;
[~, sub] = max(mask, [], 1);
sub = sub-1;
sub(sub==0) = size(difference,1);
index = sub2ind(size(difference), sub, 1:size(difference,2));
result = difference(index);
0 Comments
See Also
Categories
Find more on Electrophysiology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!