我有一組來自 Abaqus 建模軟體的元素,每個元素都有 8 個節點(如圖 https://postimg.cc/Mfy7zjsc 所示)。它們由一個 53x9 矩陣表示,其中第一列存盤元素的名稱(編號),其他 8 個存盤節點編號。我需要按照它們在模型中出現的順序對這些元素進行排序,因為它們被賦予了相當多的亂數(節點也是如此)。然后我想讓我的 Matlab 腳本識別矩陣中的哪一行是邊界元素之一(只有 4 個節點編號在其他行之一中重復的元素),將其設定在結果的開頭矩陣,然后找到接觸它的元素(共享其他 4 個節點)并稍后跟進。
我已經有一段時間沒有編程了,我正在努力正確地撰寫回圈。我試圖讓 Matlab 首先取矩陣的一行,取一個元素并遍歷所有其他元素以找到重復項。如果它沒有找到任何并且行號與正在處理的行號不同,它應該增加一個計數器變數。如果變數達到 4,則腳本應回傳行號,這將是起始元素。現在我得到了相當隨機的結果,并且發現很難除錯它。'elem' 是包含我擁有的所有值的矩陣,'nodess' 是包含洗掉元素值的列的同一個矩陣(我知道它可能是愚蠢和無用的,但 nvm)。'E' 應該是結果矩陣。
% input data
in = [135346 135912 135913 20605 20606 51236 51237 8677 8678;...
135347 135913 135914 20604 20605 51237 51238 8676 8677;...
135348 135914 135915 20603 20604 51238 51239 8675 8676;...
135349 135915 135916 20602 20603 51239 51240 8674 8675;...
135350 135916 135917 20601 20602 51240 51241 8673 8674;...
135351 135917 135918 20600 20601 51241 51242 8672 8673;...
135352 135918 5057 107 20600 51242 5229 111 8672;...
135697 136514 20982 421 20868 136213 8585 184 20881;...
135698 136515 136514 20868 20869 136214 136213 20881 20880;...
135699 136516 136515 20869 20870 136215 136214 20880 20879;...
135700 136517 136516 20870 20871 136216 136215 20879 20878;...
135701 136518 136517 20871 20872 136217 136216 20878 20877;...
135702 136519 136518 20872 20873 136218 136217 20877 20876;...
135703 136520 136519 20873 20874 136219 136218 20876 20875;...
135704 20981 136520 20874 424 20896 136219 20875 425;...
232453 20982 192131 20715 421 8585 50247 8526 184;...
232454 192131 192132 20714 20715 50247 50248 8525 8526;...
232455 192132 192133 20713 20714 50248 50249 8524 8525;...
232456 192133 192134 20712 20713 50249 50250 8523 8524;...
232457 192134 192135 20711 20712 50250 50251 8522 8523;...
232458 192135 192136 20710 20711 50251 50252 8521 8522;...
232459 192136 192137 20709 20710 50252 50253 8520 8521;...
232460 192137 192138 20708 20709 50253 50254 8519 8520;...
232461 192138 192139 20707 20708 50254 50255 8518 8519;...
232462 192139 192140 20706 20707 50255 50256 8517 8518;...
232463 192140 192141 20705 20706 50256 50257 8516 8517;...
232464 192141 192142 20704 20705 50257 50258 8515 8516;...
232465 192142 192143 20703 20704 50258 50259 8514 8515;...
232466 192143 192144 20702 20703 50259 50260 8513 8514;...
232467 192144 192145 20701 20702 50260 50261 8512 8513;...
232468 192145 192146 20700 20701 50261 50262 8511 8512;...
232469 192146 20867 419 20700 50262 8628 183 8511;...
295505 2511 47 312217 297117 2683 51 312275 300385;...
295549 297117 312217 312216 297160 300385 312275 312276 300428;...
295593 297160 312216 312215 297203 300428 312276 312277 300471;...
295637 297203 312215 312214 297246 300471 312277 312278 300514;...
295681 297246 312214 312213 297289 300514 312278 312279 300557;...
295725 297289 312213 312212 297332 300557 312279 312280 300600;...
295769 297332 312212 312211 297375 300600 312280 312281 300643;...
295813 297375 312211 312210 297418 300643 312281 312282 300686;...
295857 297418 312210 312209 297461 300686 312282 312283 300729;...
295901 297461 312209 312208 297504 300729 312283 312284 300772;...
295945 297504 312208 312207 297547 300772 312284 312285 300815;...
295989 297547 312207 312206 297590 300815 312285 312286 300858;...
296033 297590 312206 312205 297633 300858 312286 312287 300901;...
296077 297633 312205 312204 297676 300901 312287 312288 300944;...
296121 297676 312204 312203 297719 300944 312288 312289 300987;...
296165 297719 312203 312202 297762 300987 312289 312290 301030;...
296209 297762 312202 312201 297805 301030 312290 312291 301073;...
296253 297805 312201 312200 297848 301073 312291 312292 301116;...
296297 297848 312200 312199 297891 301116 312292 312293 301159;...
296341 297891 312199 107 5057 301159 312293 111 5229];
E=0
for i=linspace(1,y,y)
EL=elem(i,1);
ns=nodess(i,:);
for j=linspace(1,size(ns,2),size(ns,2))
a=0;
for k=linspace(1,y,y)
for l=linspace(1,size(ns,2),size(ns,2))
wn=nodess(k,l);
if k~=i && wn==ns(l)
a=a 1
end
end
end
if a==4
E(1)=elem(i,1)
end
end
end
uj5u.com熱心網友回復:
由于問題不完全清楚,我將做出幾個假設:
- 沒有元素連接到 0 個其他元素
- 沒有元素連接到超過 2 個元素
- 元素共享4個節點和僅4個節點(即你的輸入是正確的)
- 請注意,我不能假設只有一組連接元素,因為在您的示例中情況并非如此
該演算法分為兩部分。第一部分計算模型元素的鄰接矩陣。它基本上檢查元素ii和jj共享元素。
n = size(in,1);
adj_matrix = zeros(n);
for ii=1:n
for jj=(ii 1):n
% adjacency matrix. 1 when node ii is connected to node jj
adj_matrix(ii,jj) = ~isempty(intersect(in(ii,2:end),in(jj,2:end)));
end
end
% fill lower triangular block
adj_matrix = adj_matrix adj_matrix.';
現在,您可以檢查哪些元素是“尾”元素。row 中 1k的數量是連接到 element 的元素的數量k。“tail”元素是僅連接到一個其他元素的元素:
% find the index of the elements that are linked only to one other element
id_tails = find(sum(adj_matrix,2)==1);
該演算法最后一部分的核心是遍歷鄰接矩陣,一次構建一個索引的輸出向量。當您的當前元素是 elementk并且它連接到 2 個元素時,一個已經在您的輸出向量中,因此您只需選擇另一個。現在,由于您的示例中有 2 個以上的“尾部”元素,因此您需要更多的魔法才能使其作業。基本上,如果您當前的元素是“尾巴”元素,您只需跳到下一個尾巴:
% start building output index vector, with a randomly picked tail
idx_out = id_tails(1);
% Remove first tail from list
id_tails = setdiff(id_tails,idx_out);
while length(idx_out) < n
if ismember(idx_out(end),id_tails)
% If current index is a tail, next index is the next tail
idx_out(end 1) = id_tails(1);
% Remove first tail from list
id_tails = setdiff(id_tails,idx_out);
else
% If not a tail, then next index is the only one that is not yet in
% idx_out
idx_out(end 1) = setdiff(find(adj_matrix(idx_out(end),:)),idx_out);
end
end
最后,您可以使用這些索引對輸入矩陣進行排序:
E = in(idx_out,:);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/535649.html
標籤:算法软件排序矩阵阿巴库斯
