MATLAB: Substracting a number from certain section of the array

arraysif statementMATLAB and Simulink Student Suite

I have a number series 8654 elements having data in format (degree) as follows (just an example):
A = [ 1.002 2.003 3.004 4.005 339.006 341.007 342.008 343.009 1.002 2.003 3.004 4.005];
I am currently using the following code to make elements in the above array having values more than 300 substracted by 360;
if (A(:,1) > 300)
A(:,2)= 360-A(:,1);
else
A(:,2) = A(:,1);
end
to make a new array in second coloumn which looks like
A = [ 1.002 2.003 3.004 4.005 21 19 18 17 1.002 2.003 3.004 4.005];
But I am getting the same array as A = [ 1.002 2.003 3.004 4.005 339.006 341.007 342.008 343.009 1.002 2.003 3.004 4.005];
I dont know which place I am going wrong !! Guidance required !!

Best Answer

  • Try this
    A = [ 1.002 2.003 3.004 4.005 339.006 341.007 342.008 343.009 1.002 2.003 3.004 4.005];% your array
    A(A>300) = 360-A(A>300) % you want