Creating 2 new arrays for given condition

1 次查看(过去 30 天)
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);

采纳的回答

the cyclist
the cyclist 2021-9-4
keepIndex = y>0.0004;
y_new = y(keepIndex);
tTrans_new = tTrans(keepIndex)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by