I want to pick a matrix from a structure with a certain probability. For example I have a structure with two matrices for 2 different energies. And I want to pick several times some values from the matrices with the probability from "spec".
Econtainer = struct('E_low',{pro50keV},'E_high',{pro75keV});
spec = [0.3,0.7];
So in 70 percent of the cases I take "E_high" and som e values from there for my further work.
Is it a bad Idea to use "struct"?
Thank you a lot for your help.

 Akzeptierte Antwort

Matt J
Matt J am 2 Jul. 2021
Bearbeitet: Matt J am 2 Jul. 2021

0 Stimmen

edges=cumsum([0,spec]);
switch discretize(rand,edges)
case 1
selection = Econtainer.E_low;
case
selection = Econtainer.E_high;
end

2 Kommentare

Matt J
Matt J am 2 Jul. 2021
Bearbeitet: Matt J am 2 Jul. 2021
Is it a bad Idea to use "struct"?
It's not a terrible idea, but my proposal above would reduce to something more compact if you had used a cell array instead:
choices={pro50keV, pro75keV};
selection =choices{ discretize(rand,cumsum([0,spec])) };
Frauke
Frauke am 5 Jul. 2021
Thank you! This looks much more beautiful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 2 Jul. 2021

Kommentiert:

am 5 Jul. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by