Creating a Pattern of set values within a Matrix

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 comentarios

Stephen23
Stephen23 el 24 de Feb. de 2022
@Braeden Elbers: show us the matrix that you want to achieve.
Walter Roberson
Walter Roberson el 24 de Feb. de 2022
Editada: Walter Roberson el 24 de Feb. de 2022
What is the purpose of those break statements? In context, your code is equivalent to
function B = inf_analysis(i,j,k,N)
A = ones(N^2)
B = diag(diag(A))
for rows = 1:N^2
cols = 1;
B(rows,rows+4:rows+4) = -1/4;
end
for rows = 1:N^2;
cols = 1
B(3,1:rows+4) = -1/4;
end
Braeden Elbers
Braeden Elbers el 24 de Feb. de 2022
Editada: Braeden Elbers el 24 de Feb. de 2022
I took the break statemnt out, this is the code i am working with:
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;
end
end
I want a matrix with ones in the diagoinal and -1/4 in a digonal for a size (N^2,N^2) in the (N,N+1), (N, N+4), (N+1,N), & (N+3,N) position for a N = 4 matrix
Braeden Elbers
Braeden Elbers el 24 de Feb. de 2022
Editada: Braeden Elbers el 24 de Feb. de 2022
But i dont want the four extra columns added after i run the function. So for a N = 4 matrix i only want size(B) = 64x64
David Hill
David Hill el 24 de Feb. de 2022
It would be much clearer if you just showed us an example.
%% 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.
All border values of the 5x5 matrix were set and that is how i got the b matrix

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentarios

Thank you
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
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

Iniciar sesión para comentar.

Más respuestas (1)

Braeden Elbers
Braeden Elbers el 24 de Feb. de 2022
Editada: Braeden Elbers el 24 de Feb. de 2022

0 votos

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 comentarios

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
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.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 24 de Feb. de 2022

Comentada:

el 25 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by