extracting data from the matrix

I am working with big (huge) matrix. First two columns of this matrix are x and y coordinates and the rest 4 columns are some parameters (values) associated with this coordinates:
x y p1 p2 p3 p4
4.003E+05 7.162E+06 1.099E+02 -1.833E+00 1.930E-01 3.079E-03
4.003E+05 7.162E+06 1.100E+02 -1.855E+00 2.080E-01 1.005E-02
….
I have a list of x and y values and I want to extract values of p1-p4 from this matrix for these x&y coordinates. Each particular set of x&y can be found more than once in this matrix (but p1-p4 values will be different). I need to extract every set of values. I am wondering what function i can use to do it.
Thank you!
maria K

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 6 月 4 日

0 投票

A = randi(20,20,6);
A([1 3 6],1:2) = A([2 4 9],1:2)+3*eps;
x = A([1 3 7 17],1:2);
[a b] = find(all(abs(bsxfun(@minus,A(:,1:2),permute(x,[3 2 1]))) < 100*eps,2));
out = cellfun(@(x)A(x,3:end),accumarray(b,a,[],@(x){x}),'un',0);

その他の回答 (2 件)

the cyclist
the cyclist 2013 年 6 月 3 日

2 投票

You can probably do this something like this. I assume the following:
  • x_want is a vector with the x values you want.
  • y_want is a vector with the y values you want.
  • A is your big array.
xIndex = ismember(A(:,1),x_want);
yIndex = ismember(A(:,2),y_want);
xyIndex = xIndex & yIndex;
values = A(xyIndex,3:6);

1 件のコメント

Maria K.
Maria K. 2013 年 6 月 4 日
Thank you, cyclist! it's a very elegant and short solution

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

Roger Stafford
Roger Stafford 2013 年 6 月 4 日
編集済み: Roger Stafford 2013 年 6 月 4 日

1 投票

The first thing that comes to mind is to use the 'ismember' function with the 'rows' option. Let M be the (huge) n-by-6 matrix and L the x,y list.
[tf,loc] = ismember(M(:,1:2),L,'rows');
f = find(tf);
Each row of
[f,L(loc(f),:)]
gives an index into the rows of M matched with the corresponding rows of L. (It is assumed that there are no duplications in L.)
However, for the above to function properly, the matching must be exact, and I notice that you are using decimal fractions in designating elements of M. It is very easy for a pair of x,y coordinates to fail to match another supposedly identical pair due to possible rounding differences in creating them. If that is the case, then 'ismember' cannot be used. Instead I would recommend a simple for-loop to accomplish the same matching but with some allowed tolerance for tiny differences. Call the tolerated difference 'tol'.
P = zeros(?,2); % Pre-allocate a sufficiently large index matrix
c = 0; % The counter
for ix = 1:size(L,1)
t = abs(L(ix,1)-M(:,1))<tol & abs(L(ix,2)-M(:,2))<tol;
s = sum(t);
P(c+1:c+s,1) = find(t);
P(c+1:c+s,2) = ix;
c = c + s;
end
P = P(1:c,:); % Discard unused portion of P

カテゴリ

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

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by