Converting a Python code that deals with arrays to MATLAB

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-1-13
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
Yongjian Feng 2022-1-21
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

请先登录,再进行评论。

回答(1 个)

Vandit
Vandit 2023-6-27
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

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by