for loop

5 次查看(过去 30 天)
Ferd
Ferd 2012-3-23
Hey
I have 2 matrices, A and B, of same length and I have to create another variable C
A = goes from minimum to maximum to minimum (Fuel quantity)
B = contains prezeros and the remainder has some values
For example,
A = [1;2;3;4;5;6;7;8;9;10;9;8;7;6;5;4;3;2;1;0.5];
B = [0;0;0;0;0;0;0;0;0;0;0;0;5;7;3;2;0;0;0;0];
C = [A(1);A(2);A(3);A(4);A(5);A(6);A(7);A(8);A(9);A(10);A(11);A(12);A(13);A(14)-B(13);A(15)-B(14);A(16)-B(15);A(17)-B(16);A(18);A(19);A(20)];
The only limitation is that the value of A and B values are tacked on in the array after every iteration. What I have done so far is
C(1) = A(1);
C_difference(1) = A(2) – B(1);
But I am not sure when do you invoke the subtraction cause each time the values are tacked on in the respective array.
Thanks
Ferd
  3 个评论
Thomas
Thomas 2012-3-23
Are you finding the difference between the A and B vectors.
You do not need a loop if that is what you want.. MATLAB can perform element wise subtraction without loops.
Ferd
Ferd 2012-3-23
Hey Guys,
Affirmative, however, I was pondering on it for a while now, setting 0 as the first value of a New B array and then tacking on its next value (based on A and B) and then followed by simply C = A(:,1) - NewB(:,1) within the loop should work (i think). Because it's taking the updated value of A and subtracted from the previous iteration step value of B. Adding a zero would, i believe, work.
Technically, I need to find the difference between that A and the previous value B from that row onwards when B has non-zero values. Otherwise, the C is just the A values.
Thanks

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by