フィルターのクリア

Can anyone tell what is this line doing

4 ビュー (過去 30 日間)
Kamil Kacer
Kamil Kacer 2020 年 11 月 23 日
コメント済み: Kamil Kacer 2020 年 11 月 23 日
It seems these two first lines two the exact same thing can anyone help
I cannot figure out what they are doing
a = d{1}(f(1,2));
a = f(1,2);
...
f = [1 2 4];
d = {[4 2 1] [4 1 2] [4 3 5]};
  6 件のコメント
Kamil Kacer
Kamil Kacer 2020 年 11 月 23 日
> d{1}(f(1,3))
Index exceeds the number of array elements (3).
Kamil Kacer
Kamil Kacer 2020 年 11 月 23 日
gives an error

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

採用された回答

John D'Errico
John D'Errico 2020 年 11 月 23 日
編集済み: John D'Errico 2020 年 11 月 23 日
You asked again, why you cannot use f(1,3), instead of f(1,2).
LOOK CAREFULLY AT WHAT YOU HAVE. TAKE IT ONE STEP AT A TIME.
f = [1 2 4];
d = {[4 2 1] [4 1 2] [4 3 5]};
Now, what is f(1,3)? TRY IT!
f(1,3)
ans = 4
It is the number 4. How are you using it? As an index into a cell of the cell array d. So what is d{1}?
d{1}
ans = 1×3
4 2 1
Does d{1} have a 4th element? NO.
Why was this not a problem with f(1,2)? Because f(1,2) is 2.
d{1}(f(1,2))
ans = 2
d{1}(f(1,3))
Index exceeds the number of array elements (3).
The latter case fails because you cannot access the 4th element of a vector that has only 3 elements.
  3 件のコメント
John D'Errico
John D'Errico 2020 年 11 月 23 日
Excellent.
For most problems in MATLAB where you need to understand a complicated line of code, you take apart the line from the inside and work out. Figure out what the inside piece is doing. Then look backwards and outwards. In this case, it is perhaps not fully obvious, that (f(1,2)) was an index into what came before.
Kamil Kacer
Kamil Kacer 2020 年 11 月 23 日
Thats great advice thanks a lot again

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

その他の回答 (1 件)

KSSV
KSSV 2020 年 11 月 23 日
f = [1 2 4]; % this is an array can be access by f (i) where i = 1,2,3
d = {[4 2 1] [4 1 2] [4 3 5]}; % this is a cell array, can be access by d {i} where i = 1,2,3

カテゴリ

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