How to perform a basic division to every x1000 data columns?

6 ビュー (過去 30 日間)
Sara Woods
Sara Woods 2024 年 8 月 27 日
コメント済み: Umar 2024 年 8 月 28 日
Hi all!
Below, I have a piece of code that calculates half adaptation. This is, when spikes (S.std.spk) fall below half of the initial response over time (S.std.time). The matlab figure has an arrow that indicates where the response fell below half of the initial response (I just include a figure to better explain myself, I don't need a figure).
errorbar(S.std.time(1:10), S.std.spk(1:10),S.std.err(1:10), 'o--')
xlim([0 5.5 ])
xlabel('Time (s)')
ylim ([0 13 ])
ylabel('Spike count')
%The arrows indicate the trial number in which the response has fallen
% significantly below half of the response to the first tone
% presentation. LCR
HalfIniFR=S.std.spk(1)/2;
TimeBelowHalfIniFR= min(S.std.time(S.std.spk<HalfIniFR));
SpkBelowHalfIniFR= S.std.spk(S.std.spk<HalfIniFR);
hold on
p2_1=[TimeBelowHalfIniFR, SpkBelowHalfIniFR(1)+2.5];
p2_2= [TimeBelowHalfIniFR,SpkBelowHalfIniFR(1)+1];
dp2= p2_2-p2_1;
quiver(p2_2(1),p2_1(2),dp2(1),dp2(2),0)
I need to compute below half adaptation on each column (x1000) of spikes_std_mtx.mat over S.std.time but I don't know how to code it. I would like to save the x1000 results within a column. I tried this and did not work.
Thanks in advance :) !
HalfIniFR=S.std.spk(1)/2;
TimeBelowHalfIniFR= min(S.std.time(spikes_std_mtx(2:end,i)<HalfIniFR));
TimeForHalfAdaptation = [TimeForHalfAdaptation;TimeBelowHalfIniFR];
results_belowhalf(i,:)=TimeForHalfAdaptation

採用された回答

Voss
Voss 2024 年 8 月 27 日
load('S.mat')
load('spikes_std_mtx.mat')
Here is one way:
N = size(spikes_std_mtx,2);
result = NaN(N,1);
for ii = 1:N
idx = find(spikes_std_mtx(:,ii) < spikes_std_mtx(1,ii)/2, 1);
if ~isempty(idx)
result(ii) = S.std.time(idx);
end
end
result
result = 1000x1
4.5000 4.5000 4.5000 4.5000 7.0000 7.0000 7.0000 7.0000 7.0000 7.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result is NaN where spikes_std_mtx(:,ii) is never less than spikes_std_mtx(1,ii)/2. This happens in the last 329 columns of spikes_std_mtx.
idx = find(isnan(result))
idx = 329x1
672 673 674 675 676 677 678 679 680 681
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 件のコメント
Sara Woods
Sara Woods 2024 年 8 月 28 日
Thakn you so much! It really works. I used result=result(~any(ismissing(result),2),:); to erase the NaN rows, fantastic
Voss
Voss 2024 年 8 月 28 日

You're welcome!

Since result is a vector, you can simplify the NaN removal code as follows:

result = result(~ismissing(result));

サインインしてコメントする。

その他の回答 (1 件)

Umar
Umar 2024 年 8 月 27 日

Hi @Sara Woods ,

Let me addresss your query regarding, “I need to compute below half adaptation on each column (x1000) of spikes_std_mtx.mat over S.std.time but I don't know how to code it. I would like to save the x1000 results within a column”

Please see my response to your comments below.

First, I loaded the spike data from the spikes_std_mtx.mat file which is already provided in your posted comments. This file should contain a matrix where each column represents a different trial or condition. Then, create a variable to store the results for each column, specifically the time at which the spike count falls below half of the initial response. Afterwards, loop through each column, for each column in the matrix, I had to calculate the half of the initial spike count. Determine the time at which the spike count falls below this threshold and store the result in a pre-allocated results matrix. Finally, I had to make sure that if the spike count never falls below half of the initial response, I handled this gracefully (e.g., by storing NaN or a specific value).Here is the complete MATLAB code that implements the above logic:

% Load the spike data
load('spikes_std_mtx.mat'); % Ensure this file contains the variable     
spikes_std_mtx
% Assuming spikes_std_mtx is a matrix where rows are time points and   
columns are trials
% Initialize variables
numTrials = size(spikes_std_mtx, 2); % Number of trials (columns)
TimeForHalfAdaptation = NaN(numTrials, 1); % Pre-allocate results array
% Assuming S.std.time is defined and corresponds to the time points of   
spikes_std_mtx
% Example: S.std.time = linspace(0, 5, size(spikes_std_mtx, 1)); % Adjust as 
necessary
for i = 1:numTrials
  % Calculate half of the initial spike count
  HalfIniFR = spikes_std_mtx(1, i) / 2;
    % Find the time when spikes fall below half of the initial response
    belowHalfIdx = find(spikes_std_mtx(:, i) < HalfIniFR, 1, 'first'); % Get the         first index below half
    if ~isempty(belowHalfIdx)
        TimeBelowHalfIniFR = S.std.time(belowHalfIdx); % Get the corresponding 
        time
    else
        TimeBelowHalfIniFR = NaN; % If no spikes fall below half, store NaN
    end
    % Store the result
    TimeForHalfAdaptation(i) = TimeBelowHalfIniFR;
  end
% Display the results
disp('Time for half adaptation for each trial:');
disp(TimeForHalfAdaptation);

For more information on NaN function, please refer to

https://www.mathworks.com/help/matlab/ref/nan.html

Please see attached results.

So, you will see in the code that the load function imports your spike data from the specified .mat file to make sure that the variable spikes_std_mtx is correctly defined in the file. The TimeForHalfAdaptation array is initialized with NaN values to handle cases where the spike count does not fall below half. Then, for loop iterates through each trial (column) of the spike matrix and find function is used to locate the first occurrence where the spike count is less than half of the initial response. Afterwards, the results are stored in the TimeForHalfAdaptation array, which can be used for further analysis or visualization. Hope this helps answer your question.

  2 件のコメント
Sara Woods
Sara Woods 2024 年 8 月 28 日
Thank you for your very pedagogical comment!! I take your great advice into account
Umar
Umar 2024 年 8 月 28 日
Hi @Sara Woods,
Thank you for your kind words! I'm glad to hear that you found the feedback helpful. When engaging in any learning process, it's essential to reflect on advice and insights from others, as they can provide new perspectives and enhance your understanding.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by