How to make one element in a matrix the same for other elements?

1 view (last 30 days)
What I mean by the question is the following:
Say I have matrix A and my A(2,1) = constant
Is there a way that I can write A(2,1)=A(2,1)=A(2,3) and so on?
  2 Comments
Bryan Galindo
Bryan Galindo on 1 May 2016
Edited: Bryan Galindo on 1 May 2016
What I'd like to do is have one element be the same value for various other elements.
The bigger picture of what I am trying to do however, is somewhat of a diagonal 11x11 matrix.
The A(1,1) and A(11,11) are their own unique values. (The first and last elements)
The middle rows in the diagonal (k=0) is X And the 2 other diagonals (k=1 & k=-1) is y
Example if it were a 4x4
A =
8 y 0 0
y x y 0
0 y x y
0 0 y 10
Sorry, I know it's a bit confusing.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 1 May 2016
Edited: Stephen23 on 1 May 2016
>> x = 1;
>> y = 2;
>> N = 4; % size of the matrix
>> A = eye(N) * x;
>> A(1+1:1+N:end) = y;
>> A(1+N:1+N:end) = y;
>> A(1) = 8;
>> A(end) = 10
A =
8 2 0 0
2 1 2 0
0 2 1 2
0 0 2 10
  3 Comments
Bryan Galindo
Bryan Galindo on 2 May 2016
Actually I have another question.
I have a 1xn matrix that have specific values at the ends but the middle values are all the same. How could I do this?
For example
C =
5
x
x
x
x
10
Stephen23
Stephen23 on 2 May 2016
Edited: Stephen23 on 2 May 2016
One solution:
C = ones(1,n) * x;
C(1) = 5;
C(end) = 10;
or you could try:
C = [5,x*ones(1,n-2),10]
note that these have different outputs for n<3.

Sign in to comment.

More Answers (1)

Roger Stafford
Roger Stafford on 1 May 2016
You want every other element in the second row to be set equal to the first element?
A(2,:) = A(2.1);
Or do you have other elements to be set? Your description is not very clear.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!