How to keep values whose corresponding index is present?

1 回表示 (過去 30 日間)
lucksBi
lucksBi 2018 年 1 月 3 日
コメント済み: lucksBi 2018 年 1 月 3 日
Hi all
array1 = {1,1,1,0.92,0.61,0.47,0,0;1.6,1,1,1,0.6,0.5,0.4,0.8};
array2 = {[3,4,5,6];[4,5,7,8]};
I need to keep only those values in array 1 whose corresponding index value is present in array2. e.g. Result may look like this:
ResultantArray = {[1,0.92,0.61,0.47];[1,0.6,0.4,0.8]}
Thanks in advance.

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 3 日
ResultantArray = arrayfun(@(Row) cell2mat(array1(Row,array2{Row})), (1:size(array1,1)).', 'uniform', 0)
  1 件のコメント
lucksBi
lucksBi 2018 年 1 月 3 日
Yea it works.
Thanks alot

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

その他の回答 (1 件)

Star Strider
Star Strider 2018 年 1 月 3 日
Try this:
array1 = {1,1,1,0.92,0.61,0.47,0,0;1.6,1,1,1,0.6,0.5,0.4,0.8};
array2 = {[3,4,5,6];[4,5,7,8]};
ResultantArray = cellfun(@(idx) array1(:,idx), array2, 'Uni',0);
ResultantArray{1}(1,:)
ResultantArray{2}(2,:)
ans =
1×4 cell array
{[1]} {[0.9200]} {[0.6100]} {[0.4700]}
ans =
1×4 cell array
{[1]} {[0.6000]} {[0.4000]} {[0.8000]}
  1 件のコメント
lucksBi
lucksBi 2018 年 1 月 3 日
Thanks alot for answer. This also works.
Thank You

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by