# Change the sign of a Vector over N intervals

17 views (last 30 days)
Pierre Berthon on 31 Mar 2021
Edited: darova on 1 Apr 2021
Hi everyone,
I'm new to Matlab, my question may be super easy….I want to get a vector (Ytot ) witch is composed of the same function but with different signs set on différents intervals , that's not a problem for 2 or 3 vectors , but i dont know how to manage it with n intervals ?
This is an example :
Y = function_example;%%%% (define on 1 : 100)
T = [1; 9 ;17 ;...….89; 100] ;%%%(for example 24values)
A = [-1; 1 ; 1; ......…..-1]; %%% signs for each intervals
Y1 = -1*Y(1:9); %%% equal to Y between 1 and 9, and equal to 0 between 10 and 100
Y2 =1*Y(9:17);
Y24 =-1*Y(89:100);
t1 = (1: 1: 9);
t2 = (9: 1: 17);
tn = (89 :1: 100);
Ttot = [t1 +t2+...t24];
Ytot = [Y1 + Y2 + .Y24];

Rafael Hernandez-Walls on 31 Mar 2021
Use something like this:
Y=rand(1,20);
d=[1:4,8:12,17:20];
Y(d)=-Y(d)
Pierre Berthon on 1 Apr 2021
I must have say that Y is a vector define previously by a function... Furthermore, how do you define d for N intervals ? Lets explain with another example :
X =[0: 0.1 :3*pi()];
Y = abs(sin(X));
A = [1;-1;1]; % intervals were I want to change sign of vector Y, equal to the number of peaks
%% I use findpeaks to find were the peaks begin and end with locals minimum, it returnes me B
B = [0; pi(); 2*pi(); 3*pi() ]; %begins and end of each peaks % with this function sin its easy because every intervals have the same length and there ara only 3 peaks, but at the end i want to do it with a much more complicated Vector Y with intervals of different lenghts and multiple peaks ( values 1 or -1 are enter by the user of the program )
Then i wanna have sin(x) between 0 and pi , -sin(x) between pi and 2*pi, sin between 2*pi and 3*pi ect...
So thats why i thought about doing a concatanation with N intervals of N vectors Y , each Y (Y1,Y2 ect...) define oneach intervals ( B(0 : pi), B( pi : 2*pi) ect...) but its maybe not the best way to do it ...

R2018b

### Community Treasure Hunt

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

Start Hunting!

Translated by