Matlab Noob

blustori

Senior member
Mar 2, 2005
753
0
0
Hi, I am new at using matlab and I was trying to get an expression: y += b;
How would I code this in matlab?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: blustori
I've tried that but it generates an error.

it shouldn't... lemmie try.

any chance you can post what you already got?

edit: doh I uninstalled matlab

what's the error that you get?
 

blustori

Senior member
Mar 2, 2005
753
0
0
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end

isn't "temp" a scalar? but you used temp(i)
 

fawhfe

Senior member
Mar 22, 2001
442
1
0
Originally posted by: dighn
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end

isn't "temp" a scalar? but you used temp(i)

Yeah easy solution is to initialize temp as a vector (presumably you want one of size a with elements to 0) instead of a scalar by doing

temp=zeros(a);
 

ucdbiendog

Platinum Member
Sep 22, 2001
2,468
0
0
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end

whats y? another vector? what exactly are you trying to do?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: fawhfe
Originally posted by: dighn
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end

isn't "temp" a scalar? but you used temp(i)

Yeah easy solution is to initialize temp as a vector (presumably you want one of size a with elements to 0) instead of a scalar by doing

temp=zeros(a);

I thought he meant "temp" to be a sum hence the question about += eg temp = temp + x(i)*y(i)... could be anything though. need more clarification from the OP
 

ucdbiendog

Platinum Member
Sep 22, 2001
2,468
0
0
Originally posted by: fawhfe
Originally posted by: dighn
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end

isn't "temp" a scalar? but you used temp(i)

Yeah easy solution is to initialize temp as a vector (presumably you want one of size a with elements to 0) instead of a scalar by doing

temp=zeros(a);

in matlab you dont have to declare variables. since temp(i) is defined by pre-existing variables x(i) and y(i). matlab is cool like that
 

nissan720

Senior member
Dec 3, 2004
433
0
0
each of the variables (aka: vectors, scalars, whatever < technical term) need to be defined(at the start of the progam). You should also do this in a .mat file so it will run it as a program.

Such as:
y = 0
b = 0
while y <= 2000
y = y^2 + y
b = b+1
end
 

ucdbiendog

Platinum Member
Sep 22, 2001
2,468
0
0
Originally posted by: nissan720
each of the variables (aka: vectors, scalars, whatever < technical term) need to be defined(at the start of the progam). You should also do this in a .mat file so it will run it as a program.

Such as:
y = 0
b = 0
while y <= 2000
y = y^2 + y
b = b+1
end

not always true, it depends what you are doing. for while loops, yeah they need to be defined. but for for loops, they dont necessarily need to be declared. in fortran they do.
 

ucdbiendog

Platinum Member
Sep 22, 2001
2,468
0
0
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- :confused: (I'm trying to update the sum each time.)
end

are you trying to solve for the dot product of two vectors?

<- confused as to what youre trying to solve
 

blustori

Senior member
Mar 2, 2005
753
0
0
I'm trying to get the dot product of two vectors explicitly so I'm using that for-loop. Anyways, I want a scalar answer and I kind of see where I am going wrong. Can't set vectors = scalars x__x. helps!
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: blustori
I'm trying to get the dot product of two vectors explicitly so I'm using that for-loop. Anyways, I want a scalar answer and I kind of see where I am going wrong. Can't set vectors = scalars x__x. helps!

change

temp(i) = x(i) * y(i);

to

temp = temp + x(i) * y(i);
 

ucdbiendog

Platinum Member
Sep 22, 2001
2,468
0
0
Originally posted by: dighn
Originally posted by: blustori
I'm trying to get the dot product of two vectors explicitly so I'm using that for-loop. Anyways, I want a scalar answer and I kind of see where I am going wrong. Can't set vectors = scalars x__x. helps!

change

temp(i) = x(i) * y(i);

to

temp = temp + x(i) * y(i);

bingo
 

blustori

Senior member
Mar 2, 2005
753
0
0
any way to print that out to the screen? even better, set it equal to my funtion name lets say:
ans = temp;
end
 

ucdbiendog

Platinum Member
Sep 22, 2001
2,468
0
0
Originally posted by: blustori
any way to print that out to the screen? even better, set it equal to my funtion name lets say:
ans = temp;
end

i think this will work:

disp(temp). that will just print it to the command window