a =[ 7
7
7
11
11
11]
b =[ 4
4
4
4
4
4]
c =[11
11
11]
d =[ 4
4
4
7
7
7]
for i = 1:length(a)
for j = 1:(length(c))
if (b(i) == d(j))
A(i,j) = [0]
elseif (a(i) == c(j))
A(j,j) = [-1]
end
end
end
A = [0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0] % This matrix is forming.
% The matrix to form:
A = [0 0 0
0 0 0
0 0 0
-1 0 0
0 -1 0
0 0 -1]

답변 (1개)

Honglei Chen
Honglei Chen 2017년 5월 5일

1 개 추천

I don't know what your logic is but the code is behaving as expected. The first condition (b(i)==c(j)) is always satisfied so the second branch is never executed.
HTH

댓글 수: 2

Muhendisleksi
Muhendisleksi 2017년 5월 5일
편집: Muhendisleksi 2017년 5월 5일
I was trying to create this matrix.
Honglei Chen
Honglei Chen 2017년 5월 5일
편집: Honglei Chen 2017년 5월 5일
If the matrix is constant, what's wrong just typing is out, say
A = [zeros(3);-1*eye(3)]

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2017년 5월 5일

편집:

2017년 5월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by