How to use a for-loop to find the onset (first element) of trials of zeros, ones and twos, in an array of sequences of zeros, ones and twos

조회 수: 1 (최근 30일)
I am trying to make a function that finds the onsets (three different) trials (in this case, the trial starts when a new sequence of zeros, ones and twos starts in an array of numbers, this array of numbers always consists of sequences of ones, zeros and twos).
See the example of such an array (E) for a clarification:
E = [zeros(1,10) ones(1,5) zeros(1,8) 2*ones(1,4) zeros(1,12)]
This array is always a rowvector. The example consists of 5 trials, and 39 elements (10+5+8+4+12).
With the funtion, I want to find the elements (1:39) of the starts of every trial: [start_of_trial], with the corresponding value 0, 1 or 2: [trial_value]. This way, the function will be as follows:
[start_of_trial, trial_value] = find_onsets(E)
How can I use a for-loop to make this function work? I tried several things, but nothing worked properly

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2019년 3월 31일
function [start_of_trial, trial_value] = find_onsets(E)
start_of_trial = find([1;diff(E(:))~= 0]);
trial_value = E(start_of_trial);
end
  댓글 수: 1
Sterre
Sterre 2019년 3월 31일
The function works, thank you.
Although it is not necessary here, I need to have a for-loop in the function.. Can you help me with that?

댓글을 달려면 로그인하십시오.


Catalytic
Catalytic 2019년 3월 31일
No, we're not going to do your homework for you. Go away and leave us alone.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by