dividing defined variable by named variable

조회 수: 7 (최근 30일)
Liam Bates
Liam Bates 2022년 9월 28일
댓글: Image Analyst 2022년 9월 28일
everytime i try to divide a defined variable by a calculated variable, it only outputs one number multiple times. What syntax am i missing?
d=1:.2:3;
a=sqrt((d.^2)*(3^2));
Y=d./a
  댓글 수: 1
Stephen23
Stephen23 2022년 9월 28일
편집: Stephen23 2022년 9월 28일
" it only outputs one number multiple times."
Because that is the correct result for your code, which is equivalent to d./(3*d)
"What syntax am i missing?"
Because you did not state the expected output nor what you are trying to achieve, we cannot guess the syntax that is required to achieve an unstated goal.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2022년 9월 28일
Well yeah. Just look at the numbers:
d = 1 : 0.2 : 3
d = 1×11
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 2.6000 2.8000 3.0000
a = sqrt((d.^2)*(3^2))
a = 1×11
3.0000 3.6000 4.2000 4.8000 5.4000 6.0000 6.6000 7.2000 7.8000 8.4000 9.0000
Y = d ./ a
Y = 1×11
0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333
They all look right to me. Exactly what output numbers did you expect? Tell me which element of Y it got wrong and should be a different number.
  댓글 수: 3
Liam Bates
Liam Bates 2022년 9월 28일
is it just how the matrix is set up? would i need a loop instead?
Image Analyst
Image Analyst 2022년 9월 28일
You can define "a" differently then, like perhaps add noise
d = 1 : 0.2 : 3
d = 1×11
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 2.6000 2.8000 3.0000
a = sqrt((d.^2)*(3^2)) + rand(size(d))
a = 1×11
3.3071 4.0240 4.6150 5.2096 5.6304 6.3306 7.5081 8.0647 7.8299 8.5545 9.7844
Y = d ./ a
Y = 1×11
0.3024 0.2982 0.3034 0.3071 0.3197 0.3159 0.2930 0.2976 0.3321 0.3273 0.3066

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2022년 9월 28일
a=sqrt((d.^2)*(3^2))
3^2 is 9 so inside the sqrt() you have 9*d^2. sqrt() of that is sqrt(9)*sqrt(d^2). For positive d that is 3*d
Y = d ./ a
So you have d divided by 3*d. For non-zero d that is going to be 1/3 no matter what the positive value of d is. For negative d you would get -1/3. For 0 you get nan.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by