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

12 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2017-9-15
reshape(X,[],1)
for any array/matrix/vector X
  7 个评论
Guillaume
Guillaume 2017-9-15
编辑:Guillaume 2017-9-15
???
"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).
Ross Hanna
Ross Hanna 2017-9-20
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!

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2017-9-15
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 个评论
Stephen23
Stephen23 2017-9-15
编辑:Stephen23 2017-9-15
+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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by