Converting a Python code that deals with arrays to MATLAB
3 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 13 Jan 2022
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 ?
Yongjian Feng
on 21 Jan 2022
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
Answers (1)
Vandit
on 27 Jun 2023
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
0 Comments
See Also
Categories
Find more on Call Python from MATLAB 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!