Setting a set of values in a loop

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 Comments

Your question is not clear, your problem is with the for loop or with the function?
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
Linh Nguyen's "Answer" moved here:
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.
Thank you for your input. But the numbers are specifically defined. They aren't random numbers. Basically, the numbers are set and the loop needs to be done to compute the equation accordingly. E.g. loop 1 will take the values of the first defined variables (ireg1(1) = 0, jreg1(1) = -2, nreg1(1) = 0.14632971213167) and compute the equation "gammareg1".

Sign in to comment.

Answers (1)

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 Comments

That works a treat! But my specific problem is this:
ireg1(1) = 0
ireg1(2) = 0
ireg1(3) = 0
ireg1(4) = 0
ireg1(5) = 0
ireg1(6) = 0
ireg1(7) = 0
ireg1(8) = 0
ireg1(9) = 1
ireg1(10) = 1
ireg1(11) = 1
ireg1(12) = 1
ireg1(13) = 1
ireg1(14) = 1
ireg1(15) = 2
ireg1(16) = 2
ireg1(17) = 2
ireg1(18) = 2
ireg1(19) = 2
ireg1(20) = 3
ireg1(21) = 3
ireg1(22) = 3
ireg1(23) = 4
ireg1(24) = 4
ireg1(25) = 4
ireg1(26) = 5
ireg1(27) = 8
ireg1(28) = 8
ireg1(29) = 21
ireg1(30) = 23
ireg1(31) = 29
ireg1(32) = 30
ireg1(33) = 31
ireg1(34) = 32
'
jreg1(1) = -2
jreg1(2) = -1
jreg1(3) = 0
jreg1(4) = 1
jreg1(5) = 2
jreg1(6) = 3
jreg1(7) = 4
jreg1(8) = 5
jreg1(9) = -9
jreg1(10) = -7
jreg1(11) = -1
jreg1(12) = 0
jreg1(13) = 1
jreg1(14) = 3
jreg1(15) = -3
jreg1(16) = 0
jreg1(17) = 1
jreg1(18) = 3
jreg1(19) = 17
jreg1(20) = -4
jreg1(21) = 0
jreg1(22) = 6
jreg1(23) = -5
jreg1(24) = -2
jreg1(25) = 10
jreg1(26) = -8
jreg1(27) = -11
jreg1(28) = -6
jreg1(29) = -29
jreg1(30) = -31
jreg1(31) = -38
jreg1(32) = -39
jreg1(33) = -40
jreg1(34) = -41
'
nreg1(1) = 0.14632971213167
nreg1(2) = -0.84548187169114
nreg1(3) = -3.756360367204
nreg1(4) = 3.3855169168385
nreg1(5) = -0.95791963387872
nreg1(6) = 0.15772038513228
nreg1(7) = -0.016616417199501
nreg1(8) = 8.1214629983568E-04
nreg1(9) = 2.8319080123804E-04
nreg1(10) = -6.0706301565874E-04
nreg1(11) = -0.018990068218419
nreg1(12) = -0.032529748770505
nreg1(13) = -0.021841717175414
nreg1(14) = -5.283835796993E-05
nreg1(15) = -4.7184321073267E-04
nreg1(16) = -3.0001780793026E-04
nreg1(17) = 4.7661393906987E-05
nreg1(18) = -4.4141845330846E-06
nreg1(19) = -7.2694996297594E-16
nreg1(20) = -3.1679644845054E-05
nreg1(21) = -2.8270797985312E-06
nreg1(22) = -8.5205128120103E-10
nreg1(23) = -2.2425281908E-06
nreg1(24) = -6.5171222895601E-07
nreg1(25) = -1.4341729937924E-13
nreg1(26) = -4.0516996860117E-07
nreg1(27) = -1.2734301741641E-09
nreg1(28) = -1.7424871230634E-10
nreg1(29) = -6.8762131295531E-19
nreg1(30) = 1.4478307828521E-20
nreg1(31) = 2.6335781662795E-23
nreg1(32) = -1.1947622640071E-23
nreg1(33) = 1.8228094581404E-24
nreg1(34) = -9.3537087292458E-26
Function gammareg1(tau, pi)
gammareg1 = 0
For i = 1:34
gammareg1 = gammareg1 + nreg1(i) * (7.1 - pi) ^ ireg1(i) * (tau - 1.222) ^ jreg1(i)
The problem is, I don't know how or where do I set the ireg, nreg or jreg values so then once the loop is running, it will know what values to take.
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.
Thank you for the feedback. I actually thought I could simplify the question but then realized it would be easier to just provide the actual problem.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 13 Jul 2016

Commented:

on 13 Jul 2016

Community Treasure Hunt

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

Start Hunting!