How do I return the index of a vector I am defining?

3 ビュー (過去 30 日間)
Tyler
Tyler 2022 年 10 月 25 日
編集済み: Matt J 2022 年 10 月 25 日
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?

採用された回答

John D'Errico
John D'Errico 2022 年 10 月 25 日
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]
A = 5×1
1.0000 0 0 0 3.1416
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.
  1 件のコメント
Tyler
Tyler 2022 年 10 月 25 日
For my problem, e1_pw1 and e1_pri are scalars by definition - i left that out, sorry. But I can understand what youre saying. Likely, a function like this does not and cannot exist?

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2022 年 10 月 25 日
編集済み: Matt J 2022 年 10 月 25 日
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 件のコメント
Matt J
Matt J 2022 年 10 月 25 日
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
Matt J 2022 年 10 月 25 日
編集済み: Matt J 2022 年 10 月 25 日
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);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by