• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Matlab Noob

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?
 
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- 😕 (I'm trying to update the sum each time.)
end
 
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

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

isn't "temp" a scalar? but you used temp(i)
 
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); <--- 😕 (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);
 
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

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

whats y? another vector? what exactly are you trying to do?
 
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); <--- 😕 (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
 
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); <--- 😕 (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
 
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
 
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.
 
Originally posted by: blustori
x being a vector

a = size(x);
temp = 0;

for i=1:a
temp(i) = x(i) * y(i); <--- 😕 (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
 
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!
 
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);
 
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
 
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
 
Back
Top