Converting a Python code that deals with arrays to MATLAB

Hello Everyone,
I'M working on a spike sotring code in python but When I tried to convert it to MATLAB, the code did not work as expected. please can anyone help me to solve my issue ?
Python Code:
if np.max(tmpW) < max_thresh:
tmpS = np.argmax(tmpW) +i
tmpW = data[tmpS-(sw-offset):tmpS+(sw+offset)]
ss = np.append(ss, tmpS)
wf = np.append(wf, tmpW.reshape(1, sw*2), axis=0)
# Remove duplicates
ind = np.where(np.diff(ss) > 1)[0]
ss = ss[ind]
wf = wf[ind]
Matlab code:
if max(wave_form1)< max_thresh
tmp_samp = find(max(wave_form1))+i
wave_form1 = data(tmp_samp-(spike_window-offset):tmp_samp+(spike_window+offset))
spike_samp=[spike_samp tmp_samp]
wave_from = [wave_form wave_form1]
end

6 commentaires

I would tend to suspect
wave_from = [wave_form; wave_form1]
because of the axis=0
I tried but nothing worked, pleae can you help me ?. Im trying to align my spikes
You have
wave_from = [wave_form wave_form1]
should probably be
wave_form = [wave_form; wave_form1]
Notice the change of name of the destination variable, wave_from vs wave_form
yes it got fixed, but its not storing any data because of this line
wave_form1 = data(tmp_samp-(spike_window-offset):tmp_samp+(spike_window+offset));
orginal code in python:
tmpW = data[tmpS-(sw-offset):tmpS+(sw+offset)]
At the time of the error, what is size(data), and the values tmp_samp, spike_window, and offset ? Also, what are the maximum and minimum values for tmp_samp as you iterate through the loop ?
Also this line is suspicious:
tmp_samp = find(max(wave_form1))+i
Do you want this instead?
tmp_samp = find(wave_form1 == max(wave_form1))+i

Connectez-vous pour commenter.

Réponses (1)

Hello,
After assuming that 'wave_form', 'wave_form1', 'spike_samp', 'wave_from', 'max_thresh', 'data, spike_window',' offset', and 'i' are defined correctly in your MATLAB environment, the issue in the MATLAB code lies in the lines:
tmp_samp = find(max(wave_form1)) + i; %Incorrect code
tmp_samp = find(wave_form1 == max(wave_form1)) + i; %Modified code
The reason for changing the above code is to find the indices of the maximum value(s) in 'wave_form1' rather than just the first occurrence. In the original Python code, 'np.argmax(tmpW)' returns the index of the first occurrence of the maximum value in 'tmpW'. However, in MATLAB, using 'find(max(wave_form1))' would only return the index of the first occurrence of the maximum value in 'wave_form1', which may not be the desired behavior.
wave_from = [wave_form wave_form1]; %Incorrect code
wave_form = [wave_form; wave_form1]; %Modified code
The reason for changing the code is to correctly append the 'wave_form1' waveform as a new row to the 'wave_form' matrix. In the original Python code, 'wf' is an array where each row represents a waveform. In the MATLAB code, the equivalent variable is 'wave_form'. To correctly append 'wave_form1' as a new row to 'wave_form', we need to use the semicolon (;) to separate the rows.
Hope this helps.
Thankyou

Catégories

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by