How to search a matrix for all elements of a vector?
Show older comments
I am trying to search an element list for the occurance of nodes and output the elements where the nodes are occuring.
I want to find a fast integrated method, as for loops are just horribly slow.
x_mask=N(:,2) > X(i,1)-X(i,4)/2 & N(:,2)< X(i,1)+X(i,4)/2;
NX=N(x_mask,:);
y_mask=NX(:,3)> X(i,2)-X(i,5)/2 & NX(:,3)< X(i,2)+X(i,5)/2;
NY=NX(y_mask,:);
z_mask=NY(:,4)> X(i,3)-X(i,6)/2 & NY(:,4)< X(i,3)+X(i,6)/2;
NZ=NY(z_mask,:); %NZ is the Vector that contains the nodes 22500x1 of them
%(it also still contains the x,y,z coordinates but those can be ignored)
% i.e.:
% 1 0.849999300000000 0.273165000000000 -0.517777800000000
% 2 0.859999200000000 0.273165700000000 -0.517777800000000
% 3 0.849999500000000 0.223106800000000 -0.395308600000000
% 4 0.859999400000000 0.223107700000000 -0.395308600000000
% 5 0.849999700000000 0.180807400000000 -0.291975300000000
% 8 0.859999600000000 0.180808400000000 -0.291975300000000
% 9 0.849999800000000 0.146298800000000 -0.207777800000000
% 11 0.859999800000000 0.146299700000000 -0.207777800000000
% 13 0.849999900000000 0.119606900000000 -0.142716100000000
% 14 0.859999900000000 0.119607700000000 -0.142716100000000
% 16 0.850000000000000 0.100751900000000 -0.0967901000000000
% 17 0.860000000000000 0.100752600000000 -0.0967901000000000
%...
pow=zeros(length(E),2);
for n=1:length(NZ)
for e=1:length(E)
%E contains the elements-Node table 71300x8 (+ 1 indexing column that can be ignored)
% i.e. 1 57122 57123 57125 57124 71892 71889 71890 71891
% 2 56110 56062 56060 56059 71888 71885 71886 71887
% 3 56176 56172 56165 56171 71884 71881 71882 71883
% 4 55532 55533 55534 55535 71880 71877 71878 71879
% 5 55172 55173 55211 55213 71876 71873 71874 71875
% 6 52416 52411 52412 52414 71872 71869 71870 71871
% 7 56714 56715 56683 56705 71868 71865 71866 71867
% 8 54553 54554 54559 54556 71864 71861 71862 71863
% 9 54906 53303 53304 54917 71860 71857 71858 71859
%....
if(~isempty(find(E(e,2:9)==NZ(n,1))))
pow(e,2)=pow(e,2)+1; %i need to know how many nodes of NZ are part of an element
end
end
end
I am trying to use some sort of find to search the matrix, but as the dimensions do not agree.
Are there functions that can do this? Maybe i should go for tree searching?
Thank you so much for your help!
4 Comments
O Mueller
on 29 Apr 2021
If E is [71300 x 8], this must fail: E(e,2:9).
Do I understand correctly, that pow(k, 2) contains the number of occurences of the elements of NZ in E(:, 2:9)? The current code increases the element in pow by 1 if there is any match in a row of E, but does not care about the number of matchs.
Using length() is dangerous in the general case, because it does not measure a specific dimension, but take the longest one.
Such comments are horrible, if you ask others for help:
%NZ is the Vector that contains the nodes 22500x1 of them
%(it also still contains the x,y,z coordinates but those can be ignored)
For the efficiency of the code it does matter if the arrays are matrices or vectors. With such ambiguous explanations, the readers have to guess how the real input data look like.
O Mueller
on 29 Apr 2021
Accepted Answer
More Answers (0)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!