Look up a value in another array

I have one array which lists the max values for column of column B for each unique item in column A as follows:
'888888888888888888880001' -40
'888888888888888888880002' -47
I have the following array and I would like to find the column A and column B that match with the first row in the above array, and create a new array which adds the column C value for: '888888888888888888880001' -40 to the array. Then do the same thing for the second row in the table above and add it to the same array.
'888888888888888888880001' -66 9
'888888888888888888880001' -52 10
'888888888888888888880001' -47 20
'888888888888888888880001' -56 26
'888888888888888888880001' -47 38
'888888888888888888880001' -51 42
'888888888888888888880001' -40 47
'888888888888888888880001' -40 54
'888888888888888888880002' -50 44
'888888888888888888880002' -47 57
The result of this should be an array of 2 rows:
'888888888888888888880001' -40 47
'888888888888888888880002' -47 57
There are two places that have -40 and I just need it to have either of those values in the final array, but not both.
Any help would be greatly appreciated. Thanks.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 11 月 12 日
編集済み: Andrei Bobrov 2013 年 11 月 12 日

0 投票

EDIT
d1 = [data(:,1), cellfun(@num2str,data(:,2),'un',0)];
a1 = [A(:,1),cellfun(@num2str,A(:,2),'un',0)];
[lk,i2] = ismember(data(:,1),A(:,1));
d1 = [i2(lk) cell2mat(data(lk,2:3))];
a1 = [(1:size(A,1))', cell2mat(A(:,2))];
[lc2,ii2] = ismember(d1(:,1:2),a1,'rows');
data1 = data(lc2,:);
% corrected below
idx = accumarray(ii2(lc2),(1:size(d1(lc2,:),1))',[],@(x)x(randi(numel(x))));
out = data1(idx,:);

5 件のコメント

Aaron
Aaron 2013 年 11 月 12 日
Everything works on your code except the idx line. It gives the following error message:
??? Error using ==> randperm Too many input arguments.
Error in ==> @(x)x(randperm(numel(x),1))
Error in ==> AutomatedResults_V2 at 36 idx = accumarray(ii2(lc2),(1:size(d1(lc2,:),1))',[],@(x)x(randperm(numel(x),1)));
Do you know how to fix this issue? Other than that one line it works just how I want it to though, thank you very much
Aaron
Aaron 2013 年 11 月 12 日
And also, why is randperm being used in this? I dont see what it would be used for but I am very new to matlab
Andrei Bobrov
Andrei Bobrov 2013 年 11 月 12 日
Hi Aaron! I corrected.
Aaron
Aaron 2013 年 11 月 12 日
Thank you very much! It was very helpful. If you wouldn't mind, could you look at the main question that I had for this data. I asked the question at the following link:
It is the last thing I have to do with this data and would help me out a lot.
Aaron
Aaron 2013 年 11 月 13 日
And I made it look like a long question but it is really the same question as the one I just asked on this one, only it involves using the standard deviation to remove unwanted rows

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2013 年 11 月 12 日

コメント済み:

2013 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by