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

1 visualización (últimos 30 días)
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 comentarios
CS Researcher
CS Researcher el 1 de Mayo de 2016
Can you explain a bit more? What exactly are you trying to do?
Bryan Galindo
Bryan Galindo el 1 de Mayo de 2016
Editada: Bryan Galindo el 1 de Mayo de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 1 de Mayo de 2016
Editada: Stephen23 el 1 de Mayo de 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 comentarios
Bryan Galindo
Bryan Galindo el 2 de Mayo de 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 el 2 de Mayo de 2016
Editada: Stephen23 el 2 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Roger Stafford
Roger Stafford el 1 de Mayo de 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.

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by