How can I store the result after each iteration in a row vector?
조회 수: 1 (최근 30일)
이전 댓글 표시
Below is the sum equation which is of similar form that I need to write in my Objective Function.
![I need to write this in my Objective function](https://www.mathworks.com/matlabcentral/answers/uploaded_files/579512/image.png)
I have written the following code using two 'for' loops, it's working fine but the final answer Asum(As) it is storing is only of the last iteration.
Ad = [1 2];
Ac = [1 2 3 4];
for i=1:lenght(Ac)
for j=1:length(Ad)
As=Ad(j)+Ac(i)
end
end
whereas I need the result in the form of a row vector as shown below:
As = [2 3 3 4 4 5 5 6]
Kindly help, thanks
댓글 수: 2
채택된 답변
David Fletcher
2021년 4월 10일
A quick hack would be:
Ad = [1 2];
Ac = [1 2 3 4];
As=[];
for i=1:length(Ac)
for j=1:length(Ad)
As=[As Ad(j)+Ac(i)]
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!