Converting a Python code that deals with arrays to MATLAB

조회 수: 1 (최근 30일)
Anas Wheba
Anas Wheba 2022년 1월 6일
답변: Vandit 2023년 6월 27일
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