フィルターのクリア

print values in one matrix to another

1 回表示 (過去 30 日間)
k
k 2020 年 4 月 16 日
コメント済み: Tommy 2020 年 4 月 16 日
Hi,
I have very large matricies but will abbreviate them as best as possible here.
I have a matrix called "connectivity" which contains 5884 rows (total_elements = 5884) and 5 columns. The first column is the element number, the second column is node 1 and the third column is node 2:
connectivity(total_elements, 5);
connectivity(:,1) = 1:total_elements;
connectivity(:,2) = N1;
connectivity(:,3) = N2;
making this matrix look like
1 900 175
2 175 200
3 200 145
4 145 300...
I have a second matrix "C" which has the x and y coordinates for each of the nodes.
NodeNumber Xcomponent Ycomponent
...
I need to write a loop that will search matrix "C" for the correct x and y coordinates for each node and create a new matrix "length," which looks like the following:
length = zeros(total_elements, 7);
length(:,1) = 1:total_elements
length(:,2) = N1;
length(:,3) = N2;
length(:,4) = x1; %x coordinate of node 1
length(:,5)=x2; %x coordinate of node 2
length(:,6)=y1; %y coordinate of node 1
legnth(:,7)=y2; %y coordinate of node 2
being:
element# N1 N2 x1 x2 y1 y2
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 16 日
Is it mandatory to use a for loop? This is something that is easily vectorized.
k
k 2020 年 4 月 16 日
No, a for loop isn't necessary. I essentially just need to match values from one matrix to another and print them into a final matrix.
Ex:
matrix A
node # xcoordinate ycoordinate
1 0.23 0.5
2 2.3 6.2
3 2.1 3.2
matrix B
element# N1 N2
1 2 3
2 3 1
3 2 1
matrix C, which I'm trying to create
element# N1 xcoordinate ycoordinate N2 xcoordinate yycoordinate
1 2 2.3 6.2 3 2.1 3.2
2 3 2.1 3.2 1 0.23 0.5
3 2 2.3 6.2 1 0.23 0.5
Hopefully this makes more sense! Again, I need to scale this to where the total number of elements is 5884 and the number of nodes is 2000.

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

採用された回答

Tommy
Tommy 2020 年 4 月 16 日
編集済み: Tommy 2020 年 4 月 16 日
Let me know if this works:
length = [connectivity, zeros(total_elements, 4)];
for i = 1:total_elements
length(i, [4 6]) = C(C(:,1)==connectivity(i,2),[2 3]);
length(i, [5 7]) = C(C(:,1)==connectivity(i,3),[2 3]);
end
(edit) If you want to avoid the loop, and to match the format you've given in the comments:
[~,i] = ismember(connectivity(:,[2 3]),C(:,1));
length = [connectivity(:,[1 2]), C(i(:,1),[2 3]), connectivity(:,3), C(i(:,2),[2 3])];
Either way should work for any number of elements and nodes, provided your connectivity and C matrices are formatted as you've described.
  8 件のコメント
k
k 2020 年 4 月 16 日
It worked!! Thank you so so much!!
Tommy
Tommy 2020 年 4 月 16 日
Oh awesome! Happy to help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by