How do I use the value of a variable in function?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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:
0 comentarios
Respuestas (2)
amin ya
el 25 de Jul. de 2019
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 comentarios
Star Strider
el 26 de Jul. de 2019
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 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!