Creating 2 new arrays for given condition

1 view (last 30 days)
Good day, I'm trying to write an algorithm that goes as follows:
So I have y values (y) and corresponding time values (tTrans). I want to make new arrays with one of them being y values only greater than 0.0004 and another with it's corresponding time values. however I keep getting the error:
Please help and thanks in advance!
#######################################################
Array indices must be positive integers or logical
values.
Error in Practical1 (line 74)
yn(i)=yT(r(i));
#######################################################
Let y be a [n,1] size array
let tTrans be a [1,n] size array
########################################CODE##########################################
for i=1:length(y)
if abs(yT(i))>0.0004
n=n+1; % Get the array posistions where y>0.001 for speech, creating n, which is the size of my second arrays.
end
end
r = zeros(1,n);
yn= zeros(1,n); %Initializing sizes of my second arrays
tT= zeros(1,n);
for i=1:n
if abs(yT(i))>0.0004;
for j=1:n
r(j)=i; % Get the array posistions where y>0.001
end
end
end
pause;
for i=0:n
yn(i)=yT(r(i)); %Wanting to create a new array, with all the y-values greater than 0.004;
tT(i)=tTrans(r(i)); %Wanting to create a new array, for the time values corresponding to values greater than 0.004;
end
yn=transpose(yn);
hold off;
plot(tT,yn);

Accepted Answer

the cyclist
the cyclist on 4 Sep 2021
keepIndex = y>0.0004;
y_new = y(keepIndex);
tTrans_new = tTrans(keepIndex)

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!