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.

Answers (2)

It is not possible to index an array without using linear indexing. All MATLAB arrays are implemented using linear indexing at some level.
It is easy to use toeplitz to generate logical indices for a tridiagonal matrix:
>> 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

Asked:

on 12 Oct 2015

Edited:

on 12 Oct 2015

Community Treasure Hunt

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

Start Hunting!