Application of unique in the given code
4 次查看(过去 30 天)
显示 更早的评论
The below mentioned is my element connectivity matrix for one hexagon. I have used loop to get the whole element connectivity matrix for a 8x8 hexagon structure.However I am unable to use unqie function on this.I tried using unique , but maybe i have used wrongly. Please can someone figure it out?
ny = 8 % number of hexagons in y direction
nx = 8 % number of hexagons in x direction
%Element connectivity of unit hexagon
Ele_unithex = [1 2;2 3;3 4;4 5;5 6;6 1];
%Element table
Ele_yspacing = 0:6:(6*ny);
Ele_ystack = zeros(length(Ele_unithex)*ny,2);
for i = 1:length(Ele_yspacing)-1
if i == 1
Ele_ystack = Ele_unithex + Ele_yspacing(i);
else
Ele_ystack = [Ele_ystack; Ele_unithex + Ele_yspacing(i)];% It means adding 6 to the previous hex element number
end
end
Ele_xstack = Ele_ystack;%Once stacked in y direction, now it will try to get stacked in x direction.
for i = 1:nx-1
Ele_xstack = [Ele_xstack; repmat(Ele_ystack,1) + length(Ele_xstack)];
end
%Element connnectivity of stacked hexagons
Elements_stacked = Ele_xstack;
%Usage of temporary counter in the form of temp
temp=1;
for i = 1:size(Elements_stacked,1)
%Element connectivity table for stacked hexagons
Elements(temp,1:3) = [i,Elements_stacked(i,:)];
temp = temp+1;
end
[unique_element,iaa,icc] = unique((Elements_stacked),'rows','stable')
0 个评论
回答(1 个)
Jon
2023-6-16
I don't understand the details of your application, but there doesn't seem to be any problem with your use of unique here. What maybe is surprising, and points to some other issue in your code, is that the rows of Elements_stacked are in fact already all unique (no repeats) so unique_elements is in fact identical to Elements_stacked. Further I will note that the first column of Elements_stacked is simply 1:384 so that column is unique and the second column of Elements_stacked also consists of exactly the numbers 1:384 only reordered. So those are also all unique. So if you are expecting redundant (non-unique elements) in Element_stacked, I suggest you look earlier in your code at how you construct Elements_unique to see why you are not creating redundant elements.
2 个评论
Jon
2023-6-16
Glad that helped. If that anwered your question please mark it as answered. Otherwise please say if you need more help
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!