How to make output single column in the same line as your calculation

Hi there. Whether my google and matlab searching is just crap or it hasn't been asked i can't find the answer to the above question. I've found that you can make something into one column using A(:) or 'reshape', but i wan't to make a single column vector in the same line as the calculation to avoid having a second variable for each i create. e.g. if i could write A=(sqrt(B^2+C^2))(:), that would be great but matlab doesn't accept that. at the moment i am having outputs with 81 columns which is causing Nans in my code with the error saying
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN
hat i have done is put in a column vector of 81 numbers, applied the cosine rule rearranged for the angle and it outputs with 81 columns. So a second question, which i have just thought of when writing this, which may precede the first is, 'why is matlab outputting 81 full columns for what should only be a 1*81 column vector?' My code is below,
clear
clc
Spec_Sheet_CR500
theta_input=degtorad((-10:0.25:10))
theta=theta_input(:)
sh_hyp=sqrt(sh^2+so^2)
sh_a=atan(so/sh)
sh_b=atan(sh/so)
lmh_hyp=sqrt(lmh^2+lmv^2)
lm_to_sm=sqrt((lh+sh)^2+(lo-so)^2)
beta_1=sqrt(sh_hyp^2+lmh^2-2*sh_hyp*lmh*cos(sh_b+theta))
beta_1_A=(acos((sh_hyp^2+beta_1.^2-lmh^2)/(2*sh_hyp*beta_1)))
beta_1_B=acos((lmh^2+beta_1.^2-sh_hyp^2)/(2*lmh*beta_1))
beta_2=sqrt(beta_1.^2+lmv^2-2.*beta_1.*lmv.*cos(beta_1_B+(pi/2)))
beta_2_A=acos((beta_1.^2+beta_2^2-lmv^2)/(2*beta_1.*beta_2))
beta_2_B=acos((beta_2^2+lmh_hyp^2-sh_hyp^2)/(2*beta_2*lmh_hyp))
gamma_1=sqrt(lm_to_sm^2+beta_2^2-2*lm_to_sm*beta_2*cos(beta_1_A-beta_2_A-sh_a-psi))
alpha_1_A=acos((link3^2+gamma_1^2-link_arm^2)/(2*link3*gamma_1))
alpha_2_A=acos((lmh_hyp^2+gamma_1^2-lh_hyp^2)/(2*lmh_hyp*gamma_1))
ksi=alpha_2_A+(alpha_1_A-link2_A)
H=sqrt(link1^2+lmh_hyp^2-2*link1*lmh_hyp*cos(ksi))
l=sqrt(beta_2^2+link1^2-2*beta_2*link1*cos(beta_2_B+ksi))
sigma=(acos((sh_hyp^2+l^2-H^2)/(2*sh_hyp*l)))-sh_a
many thanks

 Accepted Answer

reshape(X,[],1)
for any array/matrix/vector X

7 Comments

Thanks for answering so quickly. My only question is would that mean i have to create a new variable for each original variable? I was looking to do it all on one line?
You don't have to create a new variable. For your original example, instead of
A = (sqrt(B^2+C^2))(:)
which is not valid syntax
A = reshape(sqrt(B.^2 + C.^2)), [], 1)
Also note the use of .^ instead of ^. Learn the difference between matrix square and element-wise square
I've accepted this answer as it was perfect for what i originally asked, so thank you! Any idea on why the code is giving the equivalent of a 6561x1 column vector for an input of 81x1 vector? i've applied the cosine rule rearranged for the angle and it gives me the same angle 6480 times, then after that, the numbers i need?
@Ross Hanna: you did not use the correct operators. Read the link in Guillaume's comment.
For anyone else reading this who has the same problem, i missed out a '.' before the /. should have been;
beta_1_A=reshape(acos((sh_hyp.^2+beta_1.^2-lmh.^2)./(2.*sh_hyp.*beta_1)),[],1)
Thank you Stephen, took me a while to spot that you meant the / as well. i did know you had to, just hadn't seen it.
???
"took me a while to spot". Well, I'd only spelled it out several hours before your comment!
As per my answer, the reshape was the wrong fix to the problem. Now that the real cause of the issue has been fixed, there's no need for it (the result is already the correct shape).
I had about a hundred lines of code i had written 6 months ago to accept one input value as it was the first thing i had ever done on matlab. I then tried to change it to accept vector inputs and was in the process of adding in element-wise operators, thought i had finished as i went through every line, but that one little dot eluded me. Yes you spelled it out and thankyou very much. All i meant was i added about 300 element-wise operators and missed that one. That is what i meant, not a comment on your answer in the slightest, more me needing to go to specsavers!

Sign in to comment.

More Answers (1)

Any idea on why the code is giving the equivalent of a 6561x1 column vector for an input of 81x1 vector
Typical case of a XY problem. You get a wrong output, instead of asking why you get the wrong output (wrong size that you spotted but also wrong values), you try to sort it by reshaping the output and ask help about that. Thing is: the bit that needs fixing is the wrong output.
As per my comment: Learn the difference between matrix operations and element-wise operations. That is the root of the problem. I've not looked through every line of code, the first one that has got a problem is your beta_1_A calculation. it should be:
beta_1_A = acos((sh_hyp^2 + beta_1.^2 - lmh^2) ./ (2*sh_hyp*beta_1))
The essential part is the replacement of / by ./, I've also removed the unnecessary brackets around acos and added spaces for better readability.

1 Comment

+1 for spotting and describing the problem.
@Ross Hanna: don't just write code and hope that it is correct. You should test and confirm every line to make sure that it is doing what you need it to do (use pen-and-paper, or the command line, or some other tool to check the values). Read the documentation for every command and function that you are using. Only when a line is correct move on to the next line. There is very little practical point in writing code that you do not understand what it is doing and have not checked.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 15 Sep 2017

Commented:

on 20 Sep 2017

Community Treasure Hunt

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

Start Hunting!