How to make output single column in the same line as your calculation
11 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Stephen23
on 15 Sep 2017
reshape(X,[],1)
for any array/matrix/vector X
7 Comments
Guillaume
on 15 Sep 2017
Edited: Guillaume
on 15 Sep 2017
???
"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).
More Answers (1)
Guillaume
on 15 Sep 2017
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
Stephen23
on 15 Sep 2017
Edited: Stephen23
on 15 Sep 2017
+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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!