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

조회 수: 1 (최근 30일)
Bryan Galindo
Bryan Galindo 2016년 5월 1일
편집: Stephen23 2016년 5월 2일
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
CS Researcher
CS Researcher 2016년 5월 1일
Can you explain a bit more? What exactly are you trying to do?
Bryan Galindo
Bryan Galindo 2016년 5월 1일
편집: Bryan Galindo 2016년 5월 1일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 5월 1일
편집: Stephen23 2016년 5월 1일
>> 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
Bryan Galindo
Bryan Galindo 2016년 5월 2일
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 2016년 5월 2일
편집: Stephen23 2016년 5월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 5월 1일
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.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by