Setting a set of values in a loop
Mostrar comentarios más antiguos
Hi, an example of my problem is:
a = [ an arbitrary set of values ] % contains 53 values
b = [ an arbitrary set of values ] % contains 53 values
function c(input variables)
for i = 1:53
c = a{i} + b {i};
end
So, how do I define a and b matrices in a function logically to then be set in the for loop. I am new to MATLAB but I think its easier than I think it is. I hope I've explained myself clear enough..
4 comentarios
Azzi Abdelmalek
el 13 de Jul. de 2016
Your question is not clear, your problem is with the for loop or with the function?
Stephen23
el 13 de Jul. de 2016
Why waste time with a loop anyway? There is no need to make it so complicated:
>> A = randi(9,1,23)
A =
5 7 1 9 2 2 2 2 3 1 3 7 7 8 5 5 2 4 2 6 7 3 6
>> B = randi(9,1,23)
B =
5 5 7 6 1 5 2 3 7 8 2 7 4 6 8 1 2 6 5 5 9 4 3
>> A + B
ans =
10 12 8 15 3 7 4 5 10 9 5 14 11 14 13 6 4 10 7 11 16 7 9
Stephen23
el 13 de Jul. de 2016
The problem is how to construct a function that defines the values, so when running the loop, it knows what values to take. Sorry I am confused myself so my explanation is kind of bad.
Linh Nguyen
el 13 de Jul. de 2016
Respuestas (1)
Azzi Abdelmalek
el 13 de Jul. de 2016
Write this function and save it as function_name.m
function c=function_name(a,b)
for k = 1:3
c(k)= a(k) + b (k);
end
Then to call this function, in Matlab Windows command for example:
a=[1 2 3]
b=[4 5 6]
c=function_name(a,b)
3 comentarios
Linh Nguyen
el 13 de Jul. de 2016
Adam
el 13 de Jul. de 2016
You should try to make sure your original question covers the actual situation you want an answer to. Giving a simpler example is good, but not if it is so simple as to lead people to give answers that aren't sufficient for your actual problem. Giving full information in the comment of an answer is a lot less helpful.
Linh Nguyen
el 13 de Jul. de 2016
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!