i have a struct which consist of 10 sets of data, i.e struct(1), struct(2).......
I need to extract certain bits of data from each data set to use in a formula.
as an example:
struct(1).Mass = 200 struct(1).Distance=50
struct(2).Mass = 350 struct(2).Distance=100
struct(3).Mass = 400 struct(3).Distance=150
now if i had an equation such as: if struct(1) %<<<<<<The user selects their struct
Mass1=200
Mass2=(i) %<< this cycles and presents all the possibilities for Mass. i.e 350, 400
similar with distance. If struct(1)
Distance1=50
Distance2=(j) %<<<<<<This cycles and presents all possibilities for Distance. i.e 100, 150
If the equation was: (Mass1 * Mass2) / (Distance 1 - Distance 2).
How can i make it loop so that it presents all of the output as a list. such as:
input(struct(1))
(200 * 350) / (50 - 100) = -1400
(200 * 400) / (50 - 150) = -800
As i stated i would have 10 of these lines. the example scenario is to help me grasp the concept of whats needed to implement a loop system that presents the list of solved equation as shown above.
I would appreciate any help regarding this.

Respuestas (1)

Stephen23
Stephen23 el 26 de En. de 2019
Editada: Stephen23 el 26 de En. de 2019

0 votos

This is MATLAB, so it is simpler to avoid using a loop:
>> S(1).Mass = 200; S(1).Dist=50;
>> S(2).Mass = 350; S(2).Dist=100;
>> S(3).Mass = 400; S(3).Dist=150;
>> idx = 1; % user selected index.
>> idv = setdiff(1:numel(S),idx);
>> num = (S(idx).Mass * [S(idv).Mass]);
>> den = (S(idx).Dist - [S(idv).Dist]);
>> out = num ./ den
out =
-1400 -800
Read about how this works:

3 comentarios

Alias:Max
Alias:Max el 26 de En. de 2019
Editada: Walter Roberson el 28 de En. de 2019
I am still struggling. Perhaps a better explanation of what i want to achieve.
i have a struct labelled solar.
In solar it has 10 data sets each set has 4 variables:
# Name, (Mass(m), Diameter, Distance (d) ) each name has these 3 variables of numeric data.
1 Sun
2 Mercury
3 Venus
4 Earth
5 Mars
6 Jupiter
7 Saturn
8 Uranus
9 Neptune
10 Pluto
The user inputs the name they want i.e (sun)
now this uses a formula
F=G*(m1*m2)/((d1 - d2)^2) %<<< formula for gravitational %force(measured in Newtons)
But it requires all mass(m) and distance(d) for each name excluding the input name.
I need it to cycle / loop so the end result would be listed as:
input (Sun)
The gravitational force between the Sun and other celestial bodies are:
Mercury 123N %<< these are example values
Venus 234N
Earth 345N
Mars 456N
Jupiter 567N
Saturn 678N
Uranus 789N
Neptune 891N
Pluto 912N
This list is then going to use a sort function to display the list in descending order.
My effort is:
CB1 %<<< user input variable
if CB1(1,1)=='S' <<<< Sun
m1=solar(1).Mass
m2=solar(i).Mass
d1=solar(1).Distance
d2=solar(j).Distance
elseif CB1(1,2)=='e' %<< 2nd letter in mercury, as M would conflict with %Mars
m1=solar(2).Mass
m2=solar(v).Mass
d1=solar(2).Distance
d2=solar(d).Distance
end
This is writen for each input variable.
these values should then cycle through a function, using the formula above and display the output. As a list.
Sorry for long response but ithought it would provide better clarity.
Thanks in advance
Steven Lord
Steven Lord el 27 de En. de 2019
I would instead store the data in a table array with appropriate RowNames. See for example the "Specify Row Names" example on the table documentation page for an illustration of how to set and use row names.

Iniciar sesión para comentar.

Productos

Versión

R2016b

Preguntada:

el 26 de En. de 2019

Editada:

el 28 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by