Take a lineout of a matrix, defined by a function

2 ビュー (過去 30 日間)
Noah Chrein
Noah Chrein 2015 年 7 月 6 日
コメント済み: Noah Chrein 2015 年 7 月 6 日
Let M be an m by n matrix, let f:(1:m)->(1:n)
how can I create a vector that contains all the points that f hits?
Example:
Let:
M = [1 2 3;
4 5 6;
7 8 9]
and let j = f(i) = i
that is, my output vector should be v = [1 5 9]
or let j = f(i) = 4-i
in this case, v = [7 5 3]
or, more complicated, let j = f(i) = round(1+sqrt(i))
then v = [4 5 9]

採用された回答

Thorsten
Thorsten 2015 年 7 月 6 日
編集済み: Thorsten 2015 年 7 月 6 日
i = 1:size(M,1);
f = @(i) i;
M(sub2ind(size(M), i, f(i)))
f = @(i) 4 - i;
M(sub2ind(size(M), i, f(i)))
f = @(i) round(1+sqrt(i));
M(sub2ind(size(M), i, f(i)))
  1 件のコメント
Noah Chrein
Noah Chrein 2015 年 7 月 6 日
This works! Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by