How do I return the index of a vector I am defining?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have the following code:
e1_v1_parameters = [e1_pw1; ...
e1_pri; ...
withinBounds(e1_sc_upper, e1_sc_lower, measured_point(3)); ...
e1_abw; ...
withinBounds(e1_freq_upper, e1_freq_lower, measured_point(5)); ...
e1_obw1];
I am creating this vector. And the value inside element 3 and 5 are determined by a function I created, withinBounds.
Is there an easy way to return the index of the vector I am defining. For example, where i have written 3, I would like a function that returns the number 3 because I am in element 3 of e1_v1_parameters?
0 Commenti
Risposta accettata
John D'Errico
il 25 Ott 2022
You want the function withinBounds to know WHERE in the vector the reslts of that function are being stuffed?
This goes against the MATLAB programming paradigm. A function does not care what you do with the result of that function, nor does it know.
Anyway, that information could be impossible to know, at least in advance. For example, you have
e1_v1_parameters = [e1_pw1; ...
e1_pri; ...
withinBounds(e1_sc_upper, e1_sc_lower, measured_point(3)); ...
e1_abw; ...
withinBounds(e1_freq_upper, e1_freq_lower, measured_point(5)); ...
e1_obw1];
But how should the function KNOW that e1_pw1 and e1_pri are scalars? What if they just happen to be vectors of length 2, or length 100? Then what you return from withinBounds might end up in any number of places. For example:
A = [ones(randi(2,1),1);zeros(randi(3,1),1);pi]
What element will the number pi fall in? If I run that same code again, the anwer will likely be completely different. So if you think the function generating that 3rd parameter can know exactly where it will end up in the resulting vector, you are wrong.
Più risposte (1)
Matt J
il 25 Ott 2022
Modificato: Matt J
il 25 Ott 2022
In normal Matlab practice, you would vectorize withinBounds so that the operation could be done like this:
e1_v1_parameters = [e1_pw1; ...
e1_pri; ...
nan; ...
e1_abw; ...
nan; ...
e1_obw1];
loc=[3,5];
Upper= [e1_sc_upper, e1_freq_upper];
Lower= [e1_sc_lower, e1_freq_lower];
e1_v1_parameters(loc)=withinBounds(Upper,Lower, measured_point(loc));
3 Commenti
Matt J
il 25 Ott 2022
Some part of your code prior to what you've shown us has to make a decision on where things will be placed. So, it should be safe for us to assume that "loc" already exists.
Matt J
il 25 Ott 2022
Modificato: Matt J
il 25 Ott 2022
That doesn't mean, however, that you have to be typing indices manually. Here's an alternative example where a vector x is created with Gaussian random values interleaved with uniform random values. No non-vectorized manual entry is involved.
N=1e6;
x=nan(1,N);
locGauss=find(randi([0,1],1,N)); %locations of Gaussian samples
locUniform=setdiff(1:N,locGauss); %locations of Uniform samples
M=numel(locGauss);
x(locGauss)=randn(1,M);
x(locUniform)=rand(1,N-M);
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!