Hello,
I have the following for loop, where pairs(500x10), is there to calculate cc without for loop?
for i=1:500
cc(i,:)=pairs(i,pairs(i,:));
end

4 件のコメント

Stephen23
Stephen23 2018 年 7 月 2 日
@Mantas Vaitonis: what is pairs2 ? What values do pair and pairs2 contain?
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 2 日
編集済み: Mantas Vaitonis 2018 年 7 月 2 日
Sorry, tehre was mistake, both are pairs, they contain double type values like [1 5 4 3 5].
Stephen23
Stephen23 2018 年 7 月 2 日
@Mantas Vaitonis: given that pairs contains non-integer values, what do you expect
pairs(i,pairs(i,:));
to do?
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 2 日
Those values suppose to be integer, i fixed my commnet, and should return valuse by index stored in pairs.

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

 採用された回答

Jan
Jan 2018 年 7 月 2 日
編集済み: Jan 2018 年 7 月 2 日

0 投票

s = size(pairs);
index = sub2ind(s, repmat((1:s(1)).', 1, s(2)), pairs)
cc = pairs(index)

3 件のコメント

Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 2 日
Unfortunately, it is not what I am looking for. With Your code if I have pairs [5 1 4 5 1], cc should be [1 5 5 1 5] and the result with your code is [4 5 2 4 5].
Jan
Jan 2018 年 7 月 2 日
編集済み: Jan 2018 年 7 月 2 日
pairs = [5 1 4 5 1];
s = size(pairs);
index = sub2ind(s, repmat((1:s(1)).', 1, s(2)), pairs)
cc = pairs(index)
cc =
1 5 5 1 5
I don't know why you get [4 5 2 4 5]. Please try it again.
Mantas Vaitonis
Mantas Vaitonis 2018 年 7 月 2 日
Maybe I did mistake when copying your code, now it did work, thank you.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by