Finding three columns in one variable in another variable

Dear MATLAB Community,
I was wondering if you were able to help me with the following problem. I am trying to find the following coordinates (from Columns 2-4 in CourseMeshNodeLocs) in FibreOrientationPositionElement (Columns 8-10). Columns 2-4 wont exactly match Columns 8-10, hence some of of the logic functions I have learnt over the past few weeks will not help me.
I want to find a row in FibreOrientationPositionElement (Columns 8-10) that closly represents a row in CourseMeshNodeLocs (Columns 2-4), either through averaging or finding the closest one. Once the closest co-ordinates can be found, I then want to output columns 2-7 in FibreOrientationPositionElement for each row in CourseMeshNodeLocs.
What would be the best way of doing this? I had an idea regarding if and for loops defining a range which the co-ordinates need to be in, but I believe there may be another simpler way, which I can learn for the future.
Attached are some files and a code.
CoarseMeshNodeLocs = dlmread('CoarseTjointMesh.txt','',9,0); %Fibre Orientation @ Elements. Read text file, first few lines not needed
CoarseMeshNodeLocs(:,[2 3 4]) = CoarseMeshNodeLocs(:,[2 3 4])*10^-3; %Converts mm into m
FibreOrientationPositionElement = dlmread('matrix.txt','',0,0); %Fibre Orientation and Position data at elements

4 件のコメント

dpb
dpb 2020 年 1 月 12 日
"I want to find a row in FibreOrientationPositionElement (Columns 8-10) that closly represents a row in CourseMeshNodeLocs (Columns 2-4), either through averaging or finding the closest one..."
"Closesest" in what sense? Smallest difference in absolute position, minimum-maximum difference from given coordinate, ..., ? The simplest would seem to be to just compute a position from origin to each and then look at differences but not clear whether that would have produce the type of result looking for or not...
JLV
JLV 2020 年 1 月 12 日
Yes, the smallest difference in absolute position
Mohammad Sami
Mohammad Sami 2020 年 1 月 16 日
tol = 0.001; % fine tune for your needs.
% Two values, u and v, are within tolerance if abs(u-v) <= tol*max(abs([A(:);B(:)])).
% see Matlab docs for more details
LIA = ismembertol(Col_2_4,Col_8_10,tol,'ByRows',true);
JLV
JLV 2020 年 1 月 26 日
Interesting method, I will try this!

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

 採用された回答

Stephen23
Stephen23 2020 年 1 月 25 日
編集済み: Stephen23 2020 年 1 月 25 日

1 投票

I tried a simple vectorized solution of permute and sum of squares approach, but ran into "out of memory" issues:
>> C = dlmread('CoarseTjointMesh.txt','',9,0); %Fibre Orientation @ Elements. Read text file, first few lines not needed
>> C(:,2:4) = C(:,2:4)*1e-3; % Convert mm into m
>> F = dlmread('matrix.txt','',0,0); %Fibre Orientation and Position data at elements
>> B = 3*8*size(C,1)*size(F,1)
B = 13703964384
>> num2sip(B) % Oops... I don't have 14 GBytes of memory
ans = 13.704 G
However using a cell array worked without error, and is just as simple:
% split into row vectors in cell array:
tmp = num2cell(C(:,2:4),2);
% find indices of closest set of points:
fun = @(v)min(sum((v-F(:,8:10)).^2,2)); % requires >=R2016b
[dst,idx] = cellfun(fun,tmp);
% get output rows selected by those indices:
out = F(idx,2:7);
Note that the subtraction inside the anonymous function requires scalar dimension expansion, which was introduced in R2016b. For earlier versions replace the subtraction with bsxfun.

3 件のコメント

JLV
JLV 2020 年 1 月 26 日
I have tried going through this and it works, thank you!
A question, I understand you are trying the find the length of each co-ordinate (from the origin), and trying to find the smallest distance between them. Whichever value gives you the smallest difference is the index taken going forward. What if there are two coordinates on opposite sides of a quadrant, this means they have the same length from the origin. How does the script distinguish between this?
Stephen23
Stephen23 2020 年 1 月 26 日
編集済み: Stephen23 2020 年 1 月 26 日
@JLV: the calculation is for the (square of the) euclidean distance in 3 dimensions:
The calculation does not involve any "length" from the origin as it directly calculates the distance between any two points (where those points are relative to the origin is irrelevant).
JLV
JLV 2020 年 1 月 30 日
Thank you for the clarification.

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

その他の回答 (1 件)

Raunak Gupta
Raunak Gupta 2020 年 1 月 22 日

0 投票

Hi,
You may try finding difference between two matrices (here m x 3 matrices as m rows and 3 columns). From that you may try finding the max of the absolute of difference for each rows so it will result into a m x 1 vector. In that the minimum value will define the closest two rows in the two set of columns. You may use something like 10 times(This needs to be tuned according to what is required) of this lowest value and then find corresponding “similar” rows by using ismembertol. You may find below code useful.
% Here since the size is different for FibreOrientationPositionElement
% taking only the Number of elements that are there in CoarseMeshNodeLocs
FibreOrientationPosition_col_8_10 = FibreOrientationPositionElement(1:2393,8:10);
CoarseMeshNodeLocs_col_2_4 = CoarseMeshNodeLocs(:,2:4);
% Absolute difference between two matrices
difference = abs(FibreOrientationPosition_col_8_10 - CoarseMeshNodeLocs_col_2_4);
minimum_tol = min(max(difference,[],2));
% Here the maximum_tol can be tuned
maximum_tol = minimum_tol*10;
% Returns logical array of the similar rows
similarRowsIndex = ismembertol(FibreOrientationPositionElement(:,8:10),CoarseMeshNodeLocs(:,2:4),maximum_tol,'ByRows',true);
% Column 2-7 Values of FibreOrientationPositionElement for similar rows
similarRowsProperties = FibreOrientationPositionElement(similarRowsIndex,2:7);
Hope this helps.

4 件のコメント

JLV
JLV 2020 年 1 月 25 日
Hi, I will take a look when I am back at the home.
Thank you
JLV
JLV 2020 年 1 月 26 日
Thank you for the suggestion,
I have taken a look at the method and my biggest issues is
FibreOrientationPosition_col_8_10 = FibreOrientationPositionElement(1:2393,8:10);
This is because you can't simply ignore the all of the other datapoints in as they may contain relevent information required.
The two different inputs aren't necessarily in the same row to, and I believe the solution given by Stephen represents this problem accurately.
Please feel free to correct me!
Raunak Gupta
Raunak Gupta 2020 年 1 月 26 日
Hi, I also see Stephen's Method more comphrensive. I just thought in the beginning that computations will be a lot if try to rigoursly finding difference between two matrices. Anyways I guess this method will provide much exact answer.
JLV
JLV 2020 年 1 月 26 日
Thank you for your help.
Yes it is difficult to gauge the problem through text

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

カテゴリ

製品

リリース

R2019b

タグ

質問済み:

JLV
2020 年 1 月 12 日

コメント済み:

JLV
2020 年 1 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by