Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
subtracting values in arrays and holding the result
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
KSSV
2017년 3월 1일
편집: KSSV
2017년 3월 1일
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
Stephen23
2017년 3월 1일
편집: Stephen23
2017년 3월 1일
@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.
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!