Creating a Pattern of set values within a Matrix
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 24 Feb. 2022
@Braeden Elbers: show us the matrix that you want to achieve.
Walter Roberson
am 24 Feb. 2022
Bearbeitet: Walter Roberson
am 24 Feb. 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
am 24 Feb. 2022
Bearbeitet: Braeden Elbers
am 24 Feb. 2022
Braeden Elbers
am 24 Feb. 2022
Bearbeitet: Braeden Elbers
am 24 Feb. 2022
David Hill
am 24 Feb. 2022
It would be much clearer if you just showed us an example.
Braeden Elbers
am 24 Feb. 2022
Braeden Elbers
am 24 Feb. 2022
Akzeptierte Antwort
Weitere Antworten (1)
Braeden Elbers
am 24 Feb. 2022
Bearbeitet: Braeden Elbers
am 24 Feb. 2022
0 Stimmen
3 Kommentare
Walter Roberson
am 24 Feb. 2022
https://www.mathworks.com/matlabcentral/answers/1657905-creating-a-pattern-of-set-values-within-a-matrix#comment_2005530 for the modification for every 3rd.
You did not give the rule for the b vector.
Braeden Elbers
am 24 Feb. 2022
Walter Roberson
am 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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!