Creating a Pattern of set values within a Matrix
Show older comments
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
Stephen23
on 24 Feb 2022
@Braeden Elbers: show us the matrix that you want to achieve.
Walter Roberson
on 24 Feb 2022
Edited: Walter Roberson
on 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
on 24 Feb 2022
Edited: Braeden Elbers
on 24 Feb 2022
Braeden Elbers
on 24 Feb 2022
Edited: Braeden Elbers
on 24 Feb 2022
David Hill
on 24 Feb 2022
It would be much clearer if you just showed us an example.
Braeden Elbers
on 24 Feb 2022
Braeden Elbers
on 24 Feb 2022
Accepted Answer
More Answers (1)
Braeden Elbers
on 24 Feb 2022
Edited: Braeden Elbers
on 24 Feb 2022
0 votes
3 Comments
Walter Roberson
on 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
on 24 Feb 2022
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.
Categories
Find more on Matrix Indexing 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!