Read cell via indexing multiple times, without using for-loop

9 ビュー (過去 30 日間)
Timo W
Timo W 2013 年 5 月 31 日
Hello everyone!
I have a rather simple problem, but can't figure out a 'nice' solution for it, without using a for loop.
I have a cell array, which contains different options. And I have a Matrix, which tells me which of these options to use in different cases.
So I start with something like this:
Options = {'A' 'B' 'C'};
IndexMatrix = logical([
1 0 0
1 0 0
0 0 1
0 1 0]);
What I want to get is this outcome:
Choice =
'A'
'A'
'C'
'B'
This is my (simple) solution for this problem:
Data = cell(size(IndexMatrix,1),1);
for idx = 1:size(IndexMatrix,1)
Choice(idx) = Options(IndexMatrix(idx,:));
end
It works, but I'm curious if there might be a better solution, especially one without a for loop.
One idea was to use 'arrayfun', but I need to use every row of IndexMatrix on my cell, not every element.
Another idea would be to extend the cell to a second dimension, with the different options 'A' 'B' 'C' in every row, so you could do all the indexing at once. Somehow like this:
Choice = Options_extended(IndexMatrix)
But then again, I did not know how to extend the cell without using a for loop again. I was looking for something similar to what you would do with a vector. E.g.:
ones(4,1) * [1 2 3]
Anyway, I'm very interested, if anyone knows a 'clever' way to do this. Thanks!

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 5 月 31 日
編集済み: Andrei Bobrov 2013 年 5 月 31 日
C = {'A' 'B' 'C'};
Ind = logical([
1 0 0
1 0 0
0 0 1
0 1 0]);
ii = bsxfun(@times,Ind,1:numel(C));
out = C(ii(ii~=0))';
or
x = repmat(C,size(Ind,1),1);
out = x(Ind);
  1 件のコメント
Timo W
Timo W 2013 年 5 月 31 日
編集済み: Timo W 2013 年 5 月 31 日
Thanks a lot for the input! Eventually I came up with even another solution:
[idx,~] = find(IndexMatrix');
Choice = Options(idx)';
Turns out to be even about 5% faster than using bsxfun ;-)

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

その他の回答 (2 件)

Jan
Jan 2013 年 5 月 31 日
編集済み: Jan 2013 年 5 月 31 日
Options = {'A' 'B' 'C'};
Ind = logical([1 0 0; 1 0 0; 0 0 1; 0 1 0]);
C = Options(Ind * (1:3).');
Here the sum is performed implicitly by the matrix-vector multiplication.
  1 件のコメント
Timo W
Timo W 2013 年 6 月 1 日
編集済み: Timo W 2013 年 6 月 1 日
Nice! This is actually the simple and clean solution I was looking for. When I see it, it's so obvious ;-)
I already accepted Andrei Bobrov's answer, which also helpd a lot, but this one is actually the best one. Thanks.
I have one question though: Why do you use a dot, when transposing the vector (1:3) ?

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


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 5 月 31 日
idx=Options(sum(bsxfun(@times,IndexMatrix,1:3),2))

カテゴリ

Help Center および 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