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

조회 수: 4 (최근 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

답변 (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.

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


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.

카테고리

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