How can i give a number or name to each iterative in for loop?

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

Answers (2)

7 Comments

@Brwa: What is not working and what does "not working" exactly mean? If you want help, explaining any details is a good strategy.
Remember, Walter is one of the developers of the hopefully-soon-to-be-released Mind Reading Toolbox, ( http://www.mathworks.com/matlabcentral/answers/71339#comment_142227) so maybe that is enough info for Walter.
Hi guys, whats wrong did i say something wrong? well, if you think i did thats mean you misunderstood me, because i did not mean any thing un respectful. if you still think I said something bad to anyone, then Im sorry brother. be sure i did not mean any thing bad jut that link contain some code which is not doing the thing i want. and its not important to me whoever Mr Walter is I have a great repsect for him and all members here. Some members gave me alot of help, I always appreciate their help especially Mr Andrei Bobrov, Mr Roger Stafford and Mr Azzi AbdulMalek
Walter referred you to the FAQ, which recommends that you don't do what you want to do and recommends some other ways. However the FAQ still does give you the way to do it anyway (with the eval function) if you insist on disregarding the recommendations in the FAQ. But, all you said is "its not working". So we ask ourselves "_What_ is not working? He didn't say!" I'm reasonable certain the FAQ is working. Your code may not be, but you didn't share your code so we can see how you applied the code from the FAQ to your situation. So what can we say? I hope that explains the situation.
@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.
I really dont know what do you want guys? i said Sorry, So what should i can do more than this? that day i tried that code which was sujested in FAQ but i didnt got what i want but soon after that i got an idea to do it in another way, so thats why i didnt explain what is not working, because i already solved my problem. I asked above can i give name to any Iteration in the loop, well i think Matlab un able to do that. So i was interested to find the Iteration which contain the min number
X = max(C);
[y2(j), y22] = X(1,:));
[y3,y33 ] = min(y2);
Case_Number = y33 ; This Comand tells me the Iteration Number.
I have just posted a new question, I hope you guys help me with my new problem.
after i pick 1 iteration, i only want to plot a figure for that iteration, is it possible?
Thanks

Sign in to comment.

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.

Categories

Products

Asked:

on 26 May 2013

Community Treasure Hunt

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

Start Hunting!