MATLAB Answers

How do I use the value of a variable in function?

12 ビュー (過去 30 日間)
Katharine Woodruff
Katharine Woodruff 2019 年 7 月 25 日
回答済み: Star Strider 2019 年 7 月 26 日
I have an If loop:
if rollang >= 30 && pitchang >= 30
distance = ( ( (180 / rollang) + (90 / pitchang) ) / 2 ) * 100;
Z(a,b) = distance;
else
0
end
It is nested within a for loop that changes the values of rollang and pitchang. Z is a matrix of zeros. I want to put the value of "distance" in the place of that position each round of the for loop. For example the pitchangle and rollang are 20 and 30 respectively. Therefore distance will equal 600. Therefore I want to put 600 in the position (row =4, column = 5). What do I put in place of a and b in the function Z? I have tried Z((( rollang + 20 ) / 10), (( pitchang + 20 ) / 10))= distance
Hoping that it will place 600 in that spot. When I do that it says "Subscripted assignment dimension mismatch."
Maybe there is a better way to fill out the 3D matrix. Anything helps! Thank you kindly.
Here is a full picture of my code:
codeCapture.PNG

  0 件のコメント

Sign in to comment.

回答 (2 件)

amin ya
amin ya 2019 年 7 月 25 日
Post a minimal working version of your code. You don't say what a and b are!
Depends on what you want to do.
Z(a,b,c)=distance % if you have 3 variables
If you want Z be to like a function, try anonymous functions
Z=@(a,b) ( ( (180 / a) + (90 / b) ) / 2 ) * 100;

  2 件のコメント

Katharine Woodruff
Katharine Woodruff 2019 年 7 月 25 日
Hi sorry, I edited the question so it states what I'm hoping to put in for a and b
Katharine Woodruff
Katharine Woodruff 2019 年 7 月 25 日
I want a and b to correspond to the place in the matrix that I want the value of distance to be.
AKA if I want distance to be in position row=2, col =3, I would put Z(2,3) = distance... but since the position changes depending on the roll and pitch angle I cannot simply hard code that position.

Sign in to comment.


Star Strider
Star Strider 2019 年 7 月 26 日
I can’t follow what you’re doing, however ‘logical indexing’ is likely the best way to do what you want, considering the way you define what you want your ‘distance’ matrix to contain.
Try this:
roll = 0:10:180;
pitch = 0:10:90;
[rollm, pitchm] = meshgrid(roll, pitch); % ‘rollm’ = ‘roll’ Matrix, ‘pitchm’ = ‘pitch’ Matrix
rollTh = 20;
pitchTh = 20;
distance = zeros(11,20);
distance(pitchm >= 30 & rollm >= 30) = (180./rollm(pitchm >= 30 & rollm >= 30) + 90./pitchm(pitchm >= 30 & rollm >= 30))/2 * 100;
where
distance_subset = distance(1:5, 1:5)
produces:
distance_subset =
0 0 0 450 337.5
0 0 0 412.5 315
0 0 0 390 300
0 0 0 375 289.29
0 0 0 364.29 281.25
Note that you need to address your ‘distance’ matrix with respect to both dimensions (‘rollm’ and ‘pitchm’) on both sides of every assignment (equation) or the result will be a vector, not a matrix, and virtually impossible to work with.
An example of correctly doing that would be:
A = ones(size(distance)); % Always Preallocate
A(rollm >= 30 & pitchm >= 50) = distance(rollm >= 30 & pitchm >= 50);
I leave the rest to you.

  0 件のコメント

Sign in to comment.


Translated by