フィルターのクリア

Indexing with min and numel

3 ビュー (過去 30 日間)
dat
dat 2023 年 11 月 17 日
編集済み: John D'Errico 2023 年 11 月 17 日
vector1 = 1:5
vector2 = 2:7
N = max(numel(vector1), numel(vector2))
[n,I] = min([numel(vector1),numel(vector2)]) *can someone kindly explain to me how indexing in this works? How is it either going to be 1 or 2?

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 17 日
移動済み: Dyuman Joshi 2023 年 11 月 17 日
[n,I] = min([numel(vector1),numel(vector2)])
There are 2 elements in the input array, as numel() returns a scalar value.
Then min() will output the minimum value of those, and the first index of where the minimum value occurs.
"How is it either going to be 1 or 2?"
Because there are only 2 elements in the array.

その他の回答 (2 件)

John D'Errico
John D'Errico 2023 年 11 月 17 日
編集済み: John D'Errico 2023 年 11 月 17 日
vector1 = 1:5
vector1 = 1×5
1 2 3 4 5
vector2 = 2:7
vector2 = 1×6
2 3 4 5 6 7
N = max(numel(vector1), numel(vector2))
N = 6
I'm not sure what you mean by indexing. But the above seems perfectly logical. And there is no indexing involved. N is simply the larger of the two vector lengths. It must be the last line you show that is in question.
[n,I] = min([numel(vector1),numel(vector2)])
n = 5
I = 1
To understand this, you need to break it down. What did you do here?
[numel(vector1),numel(vector2)]
ans = 1×2
5 6
You created a VECTOR of length 2.
Now which element is the smaller one? The first one. That is what I tells you. Since that vector only has two elements in it, then I will only ever be 1 or 2.

Les Beckham
Les Beckham 2023 年 11 月 17 日
vector1 = 1:5
vector1 = 1×5
1 2 3 4 5
vector2 = 2:7
vector2 = 1×6
2 3 4 5 6 7
N = max(numel(vector1), numel(vector2))
N = 6
[n,I] = min([numel(vector1),numel(vector2)])
n = 5
I = 1
I'm not sure what you are asking. In the last line of code min returns 5 for n because numel(vector1) is 5 and returns 1 for I because numel(vector1) is in the first position of the vector constructed by concatenation: [numel(vector1),numel(vector2)].
Does that answer your question?

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by