Merging different arrays depending on condition

Hi
I have a following vector called cases as follows:
cases = [1,4,4,3,1,2,2,4,3,4,4,...];
Additionally, I have four arrays Array1 to Array4.
Now I want to construct a new Array, where the elements of this new array are picked from the Array1,...,Array4 depending on what the "cases" array indicates. So for instance, the first element of cases is "1" which means to pick from Array 1 (and I would like to do this in the right order, so I pick the first element of Array1). Then the second element of cases is a "4", so now I want to sample from Array 4, again the first element. The next element of cases is again "4", now I pick the second element of Array4 and so on. How can I do that effectively? The only thing which comes to my mind is to loop with ifelse loops, but that seems rather not so straightforward. Thanks

 採用された回答

Stephen23
Stephen23 2017 年 8 月 28 日
編集済み: Stephen23 2017 年 8 月 28 日

3 投票

"How can I do that effectively?"
Like always in MATLAB, using indexing is going to be simple and efficient, and storing data in lots of separate matrices just makes code complicated, so the first thing we will do is put all of the data into one cell array, then the solution is simple:
>> cases = [1,4,4,3,1,2,2,4,3,4];
>> C = {101:110,201:210,301:310,401:410};
>> arrayfun(@(c,x)C{c}(x),cases,1:numel(cases))
ans =
101 402 403 304 105 206 207 408 309 410
>>
If the matrices are the same size then you could also put them into one numeric array, which would make the code a bit more efficient:
>> M = [101:110;201:210;301:310;401:410]
M =
101 102 103 104 105 106 107 108 109 110
201 202 203 204 205 206 207 208 209 210
301 302 303 304 305 306 307 308 309 310
401 402 403 404 405 406 407 408 409 410
>> M(sub2ind(size(M),cases,1:numel(cases)))
ans =
101 402 403 304 105 206 207 408 309 410
>>

8 件のコメント

MiauMiau
MiauMiau 2017 年 8 月 28 日
編集済み: MiauMiau 2017 年 8 月 28 日
I am not so sure this is doing the right thing actually. For instance it seems that the first instance of the case "3" is not picking the first instance of 300:310 which would be 300, but the the third instance..also true for the first instance of case 4, which should be 400..hence this code is throwing me an error for my real data..
So the output for your (edited) example:
>> cases = [1,4,4,3,1,2,2,4,3,4]; >> C = {101:110,201:210,301:310,401:410};
should be:
101, 401,402, 301, 102, 201, 202,...
Stephen23
Stephen23 2017 年 8 月 28 日
編集済み: Stephen23 2017 年 8 月 28 日
"first instance of 300:310 which would be 300"
Have a look at my code. The first instance of the third "array" is 301, not 300. The first instance of the fourth "array" is 401, not 400. I did that to make the counting simpler: e.g. 301, 302, 303, etc, correspond to the first, second, third, etc, elements of the third "array".
It seems that you want to sequentially pick the elements of the vectors, regardless of the position in cases: is this correct? Your original question was ambiguous on this, and your explanation seemed to me that you wanted to pick each based on the same location as in cases. Give me a minute :)
MiauMiau
MiauMiau 2017 年 8 月 28 日
編集済み: MiauMiau 2017 年 8 月 28 日
Edit: Yes, sequentially:) Sorry for the ambiguity!
I see that you changed that, but the code is still not doing what I asked for...see above: If the first instance of the fourth array should be now 401, the output still should not be 101 402 403, ... but 101, 401, 402,...
Stephen23
Stephen23 2017 年 8 月 28 日
編集済み: Stephen23 2017 年 8 月 28 日
Using a for-loop and some simple logical indexing makes for one easy solution:
cases = [1,4,4,3,1,2,2,4,3,4]
mat = [101:110;201:210;301:310;401:410]
vec = unique(cases);
out = NaN(size(cases));
for k = 1:numel(vec)
idx = cases==vec(k);
out(idx) = mat(k,1:nnz(idx));
end
Giving:
out =
101 401 402 301 102 201 202 403 302 404
MiauMiau
MiauMiau 2017 年 8 月 28 日
many thanks. What can I do however if my arrays are not of equal length?
Stephen23
Stephen23 2017 年 8 月 28 日
編集済み: Stephen23 2017 年 8 月 28 日
Use a cell array:
cases = [1,4,4,3,1,2,2,4,3,4];
C = {101:110;201:210;301:310;401:410};
vec = unique(cases);
out = NaN(size(cases));
for k = 1:numel(vec)
idx = cases==vec(k);
out(idx) = C{k}(1:nnz(idx));
end
MiauMiau
MiauMiau 2017 年 8 月 28 日
ah ok, my own written cell-array version just had a bug then. Many thanks!:)
KL
KL 2017 年 8 月 28 日
Neat! +1

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2017 年 8 月 28 日

コメント済み:

KL
2017 年 8 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by