How to expand a small matrix to a larger matrix

Hello,
I am having a difficult time trying to figure out a loop that will expand the matrix on the left to the one on the right. Any help for this would be very appreciated!
Thank you

2 Comments

What are you inserting into the white squares? zeros, nan, or something else?

Sign in to comment.

 Accepted Answer

I think you need two for loops. One for the rows, one for the columns.
If you replace your current numbering with just integers, that might make it a little more obvious.
1x -> 1
1y -> 2
2x -> 3
...
6x -> 11
6y -> 12

7 Comments

Yes, I know I need multiple for loops and I do have the numbering of the rows and columns in mind but I can't seem to get the order to come out correctly
Please share what you have tried so we can help you.
I appreciate the help!
Its unfinished, but this is the loop I have started. I am having trouble finding a way to migrate the values in the left matrix to the right matrix as I showed in the picture above.
" Global_Stiffness_Matrix_K " is the large matrix (in the picture above on the right)
" Local_Element_1_Stiffness_Matrix " is the small matrix (on the left)
Global_Stiffness_Matrix_K = zeros(52,52);
nodalNumber = [1 ; 2 ; 3 ; 6 ; 7 ; 9 ; 10 ; 11]
for i = 1:size(nodalNumber,1)
for j = 1:size(nodalNumber,1)
Global_Stiffness_Matrix_K(nodalNumber(i), nodalNumber(j)) = Local_Element_1_Stiffness_Matrix(nodalNumber(i), nodalNumber(j));
end
end
Global_Stiffness_Matrix_K
I think you want to create a vector of nodes, kind of like you have, and have your for loops run for the length of this vector. Use the index values to select the data from your compact matrix, and then place it in your expanded matrix using index to select the location from your nodes.
nodalNumber = [1:6 11:14 17:22];
A = ones(length(nodalNumber));
B = zeros(max(nodalNumber));
for c = 1:length(nodalNumber)
for r = 1:length(nodalNumber)
B(nodalNumber(c),nodalNumber(r)) = A(c,r);
end
end
B
B = 22×22
1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
imagesc(B)
Thanks! I got the shape and values to be in the position I want them to be!
There is one more thing though, in this line:
nodalNumber = [1:6 11:14 17:22];
we knew these values because of the picture above. The problem I am solving has several compact matricies and all I am given are specific points with x and y values (hence my numbering is 1x , 1y , 2x , 2y etc.).
Is there a way to get the same structure without using the nodalNumber line and basing it off of the coordinates of the given points?
Probably. Exactly what is your starting point?
From your original code, something like this should work.
nodalNumber = [1 2 3 6 7 9 10 11];
nodalNumber = sort([2*nodalNumber 2*nodalNumber-1])
nodalNumber = 1×16
1 2 3 4 5 6 11 12 13 14 17 18 19 20 21 22
Thank you!! Yes this was what I was trying to do! Thanks for all the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 Mar 2021

Commented:

on 13 Mar 2021

Community Treasure Hunt

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

Start Hunting!