Create a logical vector

Hi All,
I have a cell array, where each cell in the first row contains a double of various lengths. Each double corresponds to sample numbers where a spike occurs (from a neuron - variable called cluster_split). I also have created an array of 0's the length of signal I am interested in (variable called LFP). I would like to index across each double (i.e.: cluster_split{1,1}) and create a logical vector of 1's and 0's.
For example, if in cluster_split{1,1} there is a value equal to 2502 (corresponding to sample 2502 in the LFP vector), then Matlab would output a 1 to the LFP array. I would like to create a separate "LFP" file for every double in the cluster_split cell array.
Thanks in advance for the help!
PK

 Accepted Answer

Adam Danz
Adam Danz on 2 Aug 2018
Edited: Adam Danz on 2 Aug 2018
Does this help?
LFP = false(1, 3000);
LFP(2500) = true;
If LFP is at millisecond resolution and a spike occurs at 2500 milliseconds, you'll see a 1 in position 2500. Since I used true/false the vector is logical.

9 Comments

PAK
PAK on 2 Aug 2018
Edited: PAK on 2 Aug 2018
Hi!
Thanks for your comment. That works in the case of just adding a "1" to position 2500 in LFP. However, what I'm looking to do is slightly different. I'll try to explain better below:
LFP is a 1 x 3000 matrix of 0s. The other matrix (cluster_class) has basically a 5000 x 1 list of sample numbers. I would like to basically return a "1" in LFP for every sample that is in cluster_class. So, for example, if in row 3000 of cluster_class, the value is "2321" that means that there should be a "1" in column "2321" of the LFP array.
Does that make sense? Sorry if I'm explaining it poorly.
Thanks!
That's clearer and if I understand you correctly, you could use my example above to solve this problem. I created fake data so we have something to work with. If the fake data do not match your formats, please clarify.
cluster_class are random integers so there will be repeats.
LFP = false(1,3000);
cluster_class = randi(3000, 5000, 1);
To assign a 1 in LFP at the indices listed in cluster_class,
LFP(cluster_class) = true;
Note that this is the same as my answer above except we're using your variable. Also note that since I've used true/false, LFP will be a logical vector.
Hi! This is perfect and works with the code you provided.
One question though:
If cluster_class is a cell array, where each cell contains the sample numbers in an array (i.e.: cluster_class{1,1}, cluster_class{1,2}, cluster_class{1,n} etc) is it possible to still use this? I've added your code into a for loop over the variable "n". I've pasted it below (in this code, clusters = n).
Thank you!!
for i = clusters
% round spikeTimes to the nearest .5 millisecond
spxtime_round = round(spikeTimes*2)/2;
% convert spikeTimes to seconds; then convert to sample number
spxtime_samples = (spxtime_round/1000) * sr;
% create an array
cluster_class_samples = [spikeClusters spxtime_samples];
% separates essentially cluster x samples; each column in cluster_split{i} is the same cluster
temp = cluster_class_samples(:,1) == i;
cluster_split{i} = cluster_class_samples(temp,2);
% create a blank array of 0's the size of the LFP timeseries; each column is a cluster
test = size(data,2);
LFP = false(1, test);
LFP(cluster_split{i}) = true;
end
Adam Danz
Adam Danz on 2 Aug 2018
Edited: Adam Danz on 2 Aug 2018
You did mention that cluster_class was a cell array and I missed that. Here's the same code for the cell array.
LFP = false(1,3000);
cluster_class = num2cell(randi(3000, 5000, 1));
LFP([cluster_class{:}]) = true;
If you're doing this in a loop,
LFP(cluster_class{i}) = true;
About your loop, there are some issues. For example, why are the first three lines executed in your loop when none of their results will change within the loop? Your 'test', 'LFP' variables will be overwritten at each iteration. From the looks of it, you don't need a loop at all.
If cluster_class is a cell array of scalar values, then it shouldn't be. It should be a vector, which will be easier to work with, use much much less memory (150 times less) and will be faster to process.
Hi,
You're right, I didn't even think about that. I took the first 3 lines out of the for loop. The below code works (I modified the LFP logical to have one row for each of the arrays in the cluster_class cell array). However, it seems to overwrite within each iteration of the for loop. Is there an easy way to prevent this?
Thanks!
PK
% get the size (in a scalar value) of the LFP vector
test = size(data,2);
% run through each of the arrays in the cluster_class cell array
for i = clusters % the cluster # corresponds to the cell array columns
LFP = false(i, test);
LFP(cluster_class{i}) = true;
end
PAK
PAK on 3 Aug 2018
Cluster_class is a cell array, with each column in the first row containing another array. Does your comment still apply? Thanks! PK
Adam Danz
Adam Danz on 3 Aug 2018
Edited: Adam Danz on 3 Aug 2018
Does each array cluster_class{n} have the same size? In other words, are all cell arrays stored in cluster_class the same length? If so, this code can be simplified.
In any case, what is the value of 'clusters'? I think what you want is something like this
LFP = false(length(cluster_class), test);
for i = 1:length(cluster_class)
LFP(cluster_class{i}) = true;
end
Hi Adam,
Each array is not the same size. The value of 'clusters' is [1,2,3,4,5,6,7,8]. Essentially the row length of cluster_class{n}. The following code below works!
LFP_size = size(data,2);
for i = clusters
LFP(i, :) = false(1, LFP_size);
LFP(i,cluster_split_recalled_samples{i}) = true;
end

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

PAK
on 2 Aug 2018

Commented:

PAK
on 3 Aug 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!