Info

This question is closed. Reopen it to edit or answer.

subtracting values in arrays and holding the result

1 view (last 30 days)
Hi all,
I have 2 vectors (inputs)
B=[10.81 10.83 10.84 10.93 11.31 11.7 11.7 11.7 11.7 11.7 11.7 11.7 11.7 11.7]';
lool=[0 0.02 0.01 0.09 0.38 0.39 0 0 0.04 0.24 0.65 0.57 -0.66 -0.54]';
when lool<0 AND B==11.7 I would like to do the calculation B+lool BUT ONLY for the first element that meet these conditions. Then I would like to use the result calculated (in this case 11.7-0.66=11.04) and add the next element from lool that meets these conditions (in this case 11.04-0.54=10.5). The final output I want to get is vector C
C= [10.81
10.83
10.84
10.93
11.31
11.7
11.7
11.7
11.7
11.7
11.7
11.7
11.04
10.5]
thanks

Answers (1)

KSSV
KSSV on 1 Mar 2017
Edited: KSSV on 1 Mar 2017
B=[10.81 10.83 10.84 10.93 11.31 11.7 11.7 11.7 11.7 11.7 11.7 11.7 11.7 11.7]';
lool=[0 0.02 0.01 0.09 0.38 0.39 0 0 0.04 0.24 0.65 0.57 -0.66 -0.54]';
C = B ;
for i = 1:length(B)
if lool(i) < 0 && B(i)==11.7
C(i) = B(i)+lool(i) ;
end
end
Vectorised:
idx = B==11.7 & lool<0 ;
C = B ;
C(idx) = B(idx)+lool(idx) ;
  9 Comments
Stephen23
Stephen23 on 1 Mar 2017
Edited: Stephen23 on 1 Mar 2017
@Nikolas Spiliopoulos: you have now asked about this topic in ten separate questions, each time giving a small tidbit of information that changes the requirements. These vague definitions and explanations, such as "So the last calculation should be 11.04-0.54 instead of 11.7-0.54" do not clarify the general rule, nor do they give us sample input and output data to work with.
It would be a much more effective use of everyone's time if you defined the problem clearly, complete with sample input and output vectors that we can test code on. Please help us to help you by finally writing one clear question with all of the information that we actually need, including the sample input and output vectors.
For example:
"I need to generate from vector X the vector Y:"
X = [1,2,3,4,...];
Y = [1,4,7,2,...];
"The rules are that"_
  • "if X(k+1)>X(k) then ... "
  • "if X(k+1)<X(k) then ...."
  • etc.
And then we can finally resolve your task for you.
PS: the sample output vector is important. Do not forget it. We need to be able to use these vectors.
Nikolas Spiliopoulos
Nikolas Spiliopoulos on 1 Mar 2017
sorry for being a pain, I am going to edit the question again thanks

Tags

Community Treasure Hunt

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

Start Hunting!