index problem while sort
1 回表示 (過去 30 日間)
古いコメントを表示
I am trying to sort some positive values from one array and finding the index of that.
Omega=[1.528 -1.528 0.792 -0.792];
[NN,ind]=sort(Omega(Omega>=0));
Answer:
NN=[0.792 1.528]
but
ind=[2,1] is coming which is wrong. It should ind=[3,1].
Please help regarding this.
0 件のコメント
採用された回答
Dyuman Joshi
2024 年 2 月 1 日
編集済み: Dyuman Joshi
2024 年 2 月 1 日
"ind=[2,1] is coming which is wrong."
It is correct.
The input to sort() only has 2 elements, so the expectation to get [3 1] as the output is misplaced -
Omega=[1.528 -1.528 0.792 -0.792];
Omega(Omega>=0)
I assume you want to get the indices of these particular values from the original array, in that case, try this -
NN=sort(Omega(Omega>=0))
[~,idx] = ismember(NN,Omega)
However, the values here are obtained directly from the original array via indexing, so ismember() works here.
その他の回答 (1 件)
VBBV
2024 年 2 月 1 日
移動済み: Dyuman Joshi
2024 年 2 月 1 日
Here is another way to get the indices without overhead of using ismember function
Omega=[1.528 -1.528 0.792 -0.792];
[NN,ind]=sort(Omega)
ind(NN>0)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!