I've got a matrix with 4 columns and I want to insert a fifth one and I want it to have either -1,1 or 0 depending on the results of the subtraction of the values from my colums 3 and 2.

354.0000 0.8058 0.0745 -0.7312
356.0000 0.8077 0.2874 -0.5203
358.0000 0.9386 0.2128 -0.7258
360.0000 0.8834 0.1159 -0.7676
362.0000 0.8750 0.2302 -0.6448
364.0000 0.5578 0.0079 -0.5499
366.0000 0.6020 0.4472 -0.1548
368.0000 0.5581 0.3495 -0.2086
For example, I subtracted the values from column 3 and 2 and added the results as a 4th column. Now I want to add a fifth column whose values will be either 1, -1 or 0 depending on the subtraction results of the values from column 3 and 2. I want my 5th column to show 1, when the C3>C2 ; -1 when the C3<C2 and 0 when the C3=C2 and I want to do it for each row in column 5.

2 Comments

Thanks for the quick feedback. It worked but gave me two new columns with ones and minus ones. How can I sort that out? EDIT: My mistake, sorry. Thank you again!

Sign in to comment.

 Accepted Answer

x(:,end+1) = double(-(x(:,4) < 0)) + double(x(:,4) > 0);
Because the - converts from logical to double, you do not need the first double; the following also works
x(:,end+1) = -(x(:,4) < 0) + double(x(:,4) > 0);

More Answers (0)

Categories

Find more on Functions in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!