How can i give a number or name to each iterative in for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
example:
A=[2 2 2; 3 3 3; 1 1 1];
B=[1 2 3];
for i = 1:3
C = B + A(1:end,i)
end
C =
3
5
4
C =
3
5
4
C =
3
5
4
i want to change C to ans_1 , ans_2, ans_3 or any other name
My main point is the answer should not have the same name, the name should be
changed with each iterative. if name is not possible i can still accept numbers
such as
ans_1 = ..................... Or 1 =
3 3
5 5
4 4
ans_2 = .......................... 2 =
3 3
5 5
4 4
댓글 수: 0
답변 (2개)
Walter Roberson
2013년 5월 26일
댓글 수: 7
Jan
2013년 5월 28일
@Brwa: We all are eager to help users, who have Matlab problems. It is obvious, that it is impossible to suggest any modifications based only on "it is not working". We neither know what is not working, if you get an error or if the results differ from your expectations.
On one hand this is very obvious, on the other hand this happens so often in this forum, that it hurts. Therefore irony is an adequate method to avoid growing aggressions. So do not take it personally, just provide the information, which are required to help us to solve your problem. Then we are serious again.
Image Analyst
2013년 5월 26일
Like the FAQ said, don't do it in a loop. If you want three variables, just do it one at a time:
ans_1 = B' + A(:, 1)
ans_2 = B' + A(:, 2)
ans_3 = B' + A(:, 3)
but if it's a larger array it will be more convenient to use indexes. Try it like this instead:
A=[2 2 2; 3 3 3; 1 1 1];
B=[1 2 3];
C=zeros(size(A));
for column = 1:3
C(:,column) = B' + A(1:end,column)
end
or maybe you could figure out how to make arrayfun() do it.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!