how can you change the values of a tridiagonal matrix of any size to any other value using indexing. For example, A=[0.9 0.1 0; 0.8 0.5 0.1; 0 0.1 0.5] . replace the tridiagonals without using linear indexing. Generate a code for any given matrix.
Show older comments
I want to use the diag function to do my indexing
Answers (2)
Walter Roberson
on 12 Oct 2015
1 vote
It is not possible to index an array without using linear indexing. All MATLAB arrays are implemented using linear indexing at some level.
>> A = [0.9 0.1 0; 0.8 0.5 0.1; 0 0.1 0.5]
A =
0.90000 0.10000 0.00000
0.80000 0.50000 0.10000
0.00000 0.10000 0.50000
>> V = zeros(1,size(A,1));
>> V(1:2) = 1;
>> X = 1==toeplitz(V) % logical index
X =
1 1 0
1 1 1
0 1 1
>> B = A(X) % extract values on diagonals
B =
0.90000
0.80000
0.10000
0.50000
0.10000
0.10000
0.50000
>> A(X) = 1:numel(B) % replace values on diagonals
A =
1 3 0
2 4 6
0 5 7
Categories
Find more on Matrices and Arrays 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!