Creating a Pattern of set values within a Matrix

11 views (last 30 days)
function B = inf_analysis(i,j,k,N)
A = ones(N^2)
B = diag(diag(A))
for rows = 1:N^2;
for cols = 1:N^2;
B(rows,rows+4:rows+4) = -1/4;
break;
end
end
I want to create a matrix with the value -1/4 is specific diagonals and locations based off a specific pattern i found. I was able to place the value in one specific diagonal but it added four more columns which i did not want.
  7 Comments
Braeden Elbers
Braeden Elbers on 24 Feb 2022
%% Ax = b system of equations found from values of inside of 5x5 matrix -->
% 3x3 matrix, using average of the four values touching the corresponding
% entry + 1
A = [1 -1/4 0 -1/4 0 0 0 0 0;
-1/4 1 -1/4 0 -1/4 0 0 0 0;
0 -1/4 1 0 0 -1/4 0 0 0;
-1/4 0 0 1 -1/4 0 -1/4 0 0;
0 -1/4 0 -1/4 1 -1/4 0 -1/4 0;
0 0 -1/4 0 -1/4 1 0 0 -1/4;
0 0 0 -1/4 0 0 1 -1/4 0;
0 0 0 0 -1/4 0 -1/4 1 -1/4;
0 0 0 0 0 -1/4 0 -1/4 1];
b = [6;3.5;6;3.5;1;3.5;6;3.5;6];
x = inv(A)*b
This is the matrix i created from setting up my system of equations. I need to create this matrix for size N^2.
Braeden Elbers
Braeden Elbers on 24 Feb 2022
All border values of the 5x5 matrix were set and that is how i got the b matrix

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2022
inf_analysis([],[],[],4)
ans = 16×16
1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 0 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 0 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0
function B = inf_analysis(i,j,k,N)
z = zeros(N^2,1);
e = ones(N^2,1);
e4 = -e/4;
B = spdiags([e4, z, e4, e, e4, z, z, e4],-3:4,N^2,N^2);
B = full(B);
end
  3 Comments
Walter Roberson
Walter Roberson on 24 Feb 2022
Slightly improved:
inf_analysis([],[],[],4)
ans = 16×16
1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 0 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 0 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0
function B = inf_analysis(i,j,k,N)
e = ones(N^2,1);
e4 = -e/4;
B = spdiags([e4, e4, e, e4, e4], [-3, -1, 0, 1, 4], N^2, N^2);
B = full(B);
end
Walter Roberson
Walter Roberson on 24 Feb 2022
inf_analysis([],[],[],4)
ans = 16×16
1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 0 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 0 0 -0.2500 1.0000 0 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 0 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 0 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 0 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 -0.2500 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 -0.2500 1.0000 0 0 0 -0.2500 0 0 0 0 0 0 0 0 0 -0.2500 0 0 1.0000 -0.2500 0 0 -0.2500 0 0
function B = inf_analysis(i,j,k,N)
e = ones(N^2,1);
e4 = -e/4;
e4m1 = e4; e4m1(3:3:end) = 0;
e4p1 = e4; e4p1(1:3:end) = 0;
B = spdiags([e4, e4m1, e, e4p1, e4], [-3, -1, 0, 1, 4], N^2, N^2);
B = full(B);
end

Sign in to comment.

More Answers (1)

Braeden Elbers
Braeden Elbers on 24 Feb 2022
Edited: Braeden Elbers on 24 Feb 2022
for the diagonals touching the diagonal containing ones, every third entry still needs to be zero. that is the only problem i am running into.
Will i use a similar method to creat the b vector to solve for x in Ax=b?
  3 Comments
Braeden Elbers
Braeden Elbers on 24 Feb 2022
the values within y, [N^2x1] matrix, need to be placed in specific locations within the array.
these are the rules that need to fill an [N^2x1]
y = zeros(N^2,1);
y1 = ((2*k)/4)+1
y2 = (k/4)+1
y3 = 1
The first 1:N terms need to follow the pattern
[y1 y2 y2 y2 y2... y1
followed by
[y2 y3 y3 y3 y3... y2
until the terms reach N^2-N:N^2
[y1 y2 y2 y2 y2... y1
Walter Roberson
Walter Roberson on 25 Feb 2022
I suggest using repelem(), such as
repelem([y1, y2, y1, y2, y3, y2, y1, y2, y1], [1, N-2, 1, 1, ?, 1, N-2, 1])
where ? is a value you would need to calculate.

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!