extracting data from the matrix
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (2)
the cyclist
il 3 Giu 2013
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 Commento
Maria K.
il 4 Giu 2013
Roger Stafford
il 4 Giu 2013
Modificato: Roger Stafford
il 4 Giu 2013
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
Categorie
Scopri di più su Resizing and Reshaping Matrices in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!