Indexing in for loop

1 view (last 30 days)
Mary Hemler
Mary Hemler on 1 Jun 2020
Edited: Rafael S.T. Vieira on 2 Jun 2020
I would like to create a new variable, cells_high_MI, that corresponds to the cellspikes index of the cells that have a high MISpikeTotal value. Here is my code for the MISpikeTotal calculation (towards the bottom of the code):
for i = 1:n_cells
spikes = cellspikes{i}; % access cellspikes
spikes = spikes./1000; % ms to secs
spikes = spikes(spikes>startTime&spikes<stopTime);
edgesT = linspace(startTime,stopTime,numel(trackingtimes)+1);
binnedSpikes = histcounts(spikes,edgesT);% sorts spikes into bins
% also bin headangle data
n_bins_angle = 60;
edgesHD = linspace(min(headangle), max(headangle), n_bins_angle +1);
[occupancy,~,angle_inds] = histcounts(headangle,edgesHD);
for iBin = 1:n_bins_angle
spikesPerAngle(iBin) = sum(binnedSpikes(angle_inds == iBin));
end
SPA(1,i)={spikesPerAngle};
% compute average firing rate for each HD angle
firing_rate = spikesPerAngle./(occupancy.*deltaT); %this vector is a TUNING CURVE!
FR(1,i) = {firing_rate};
% now compute individual pieces needed to compute mutual info
probability_density = occupancy./sum(occupancy); %proportion of time spent at each HD angle per total occupancy at all angles
PD(1,i)={probability_density};
% compute average firing rate across all HD angles
mean_rate = numel(spikes)./(stopTime-startTime);
MR(1,i) = mean_rate;
% compute mutual info between firing rate and HD
mutualInfo = sum(firing_rate .* log2(firing_rate./mean_rate) .* probability_density,'omitnan');
MIperspike = mutualInfo./(mean_rate);
%store mutual info for each cell into a matrix
mutualInfoTotal(1,i) = mutualInfo; %in bits per second
MISpikeTotal(1,i) = MIperspike;
idx = mutualInfoTotal >= 5;
highMI_sec = mutualInfoTotal(idx);
end
So, I have those values of highMI_sec that contain the mutual information values for the cells with MI values of at least 5. But what I actually want are the index numbers of the cells with at least MI values of 5, because I want to use these index numbers in the following calculations:
j=1;
for c=1:n_cells
spikes = cellspikes{c};
spikes = spikes(spikes > startTime & spikes < stopTime);
if length(spikes) >= 1000
edgesT = linspace(startTime,stopTime,numel(trackingtimes)+1);
binnedSpikes = histcounts(spikes,edgesT);
binnedSpikes = smoothdata(binnedSpikes,2,'gaussian',50);
pcaBinnedSpikes(j,:) = zscore(binnedSpikes);
j = j+1;
end
end
So I would like to use only the cellspikes that correspond to cells with an MI of at least 5 by using the indexes of the values in mutualInfoTotal.
Thanks! :)

Accepted Answer

Rafael S.T. Vieira
Rafael S.T. Vieira on 2 Jun 2020
Why don't you use idx as a vector?
for i = 1:n_cells
...
idx(i) = mutualInfoTotal >= 5;
highMI_sec = mutualInfoTotal(idx(i));
end
Then, later you can skip 0 values using:
for c=1:n_cells
if ~idx(c)
continue
end
spikes = cellspikes{c};
...
end
  2 Comments
Mary Hemler
Mary Hemler on 2 Jun 2020
I tried this, but I'm getting this error: Unable to perform assignment because the left and right sides have a different number of elements.
Here is the code:
for i = 1:n_cells
spikes = cellspikes{i}; % access cellspikes
spikes = spikes./1000; % ms to secs
spikes = spikes(spikes>startTime&spikes<stopTime);
edgesT = linspace(startTime,stopTime,numel(trackingtimes)+1);
binnedSpikes = histcounts(spikes,edgesT);% sorts spikes into bins
% also bin headangle data
n_bins_angle = 60;
edgesHD = linspace(min(headangle), max(headangle), n_bins_angle +1);
[occupancy,~,angle_inds] = histcounts(headangle,edgesHD);
for iBin = 1:n_bins_angle
spikesPerAngle(iBin) = sum(binnedSpikes(angle_inds == iBin));
end
SPA(1,i)={spikesPerAngle};
% compute average firing rate for each HD angle
firing_rate = spikesPerAngle./(occupancy.*deltaT); %this vector is a TUNING CURVE!
FR(1,i) = {firing_rate};
% now compute individual pieces needed to compute mutual info
probability_density = occupancy./sum(occupancy); %proportion of time spent at each HD angle per total occupancy at all angles
PD(1,i)={probability_density};
% compute average firing rate across all HD angles
mean_rate = numel(spikes)./(stopTime-startTime);
MR(1,i) = mean_rate;
% compute mutual info between firing rate and HD
mutualInfo = sum(firing_rate .* log2(firing_rate./mean_rate) .* probability_density,'omitnan');
MIperspike = mutualInfo./(mean_rate);
%store mutual info for each cell into a matrix
mutualInfoTotal(1,i) = mutualInfo; %in bits per second
MISpikeTotal(1,i) = MIperspike;
idx(i) = mutualInfoTotal >= 5;
highMI_sec = mutualInfoTotal(idx(i));
end
Rafael S.T. Vieira
Rafael S.T. Vieira on 2 Jun 2020
Edited: Rafael S.T. Vieira on 2 Jun 2020
Sorry, I didn't see mutualInfoTotal was already a vector, which makes idx also a vector. In such case, If you want the indexes, we can find it with
pos=find(mutualInfoTotal >= 5);
which will give the indexes that you seek.

Sign in to comment.

More Answers (0)

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!