Create an assembly matrix with ones placed from values in elemental matrix FEM

4 views (last 30 days)
I have coded a mesh generator that creates a rectangular mesh based upon user input values of height, width, divisions in the x and z axes. As well as this, I am now trying to create an assembly matrix based upon the results from my mesh generator. For this I want the matrix to have ones based upon the values from the elemental matrix, and zeros everywhere else. As an example, if I have the element matrix of:
element = [1 2 4; 1 4 3]
element = 2×3
1 2 4 1 4 3
I would then want the assembly matrix to appear as:
AssemblyMatrix = [1 0 0 0; 0 1 0 0; 0 0 0 1; 1 0 0 0; 0 0 0 1; 0 0 1 0]
AssemblyMatrix = 6×4
1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0
So each value in the element matrix gives the column where the one should appear, going from the first row downwards in order. The main issue is that the element matrix will not stay as shown in the example as if I change the divisions in the x/z axes, then the number of elements changes and so does the final element matrix, as well as the total size of the assembly matrix, so I need the code to be able to read through the element matrix to insert the ones into the correct places of the assembly matrix.
I have not been able to find a way to do this and any help would be appreciated.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 6 Jan 2023
Edited: Fangjun Jiang on 6 Jan 2023
%%
element = [1 2 4; 1 4 3];
N_Row=numel(element);
N_Col=max(element(:));
Row_index=(1:N_Row)';
Col_index=element';
Col_index=Col_index(:);
AssemblyMatrix=zeros(N_Row,N_Col);
ind=sub2ind([N_Row,N_Col],Row_index,Col_index);
AssemblyMatrix(ind)=1
AssemblyMatrix = 6×4
1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!