Unrecognized function or variable 'e'
3 次查看(过去 30 天)
显示 更早的评论
a = [1 2 3; 4 5 6; 7 8 9]
b = [7 5 6]
c = [19 23; 31 29; 17 13]
d = [e(1:2,:); f; e(3,:)]; % matrix f is inserted between the third and second row of e creating matrix d
e = [a(1,:);b(1,3);c(:,2).'] % matrix e consists of the first row of a, the second row of b and the transpose of the second column of c
f = c(:,1).' % matrix f is the transpose of the first column of matrix c
m = diag(a(:)) %
m = [diag(a); diag(b)] %
I was wondering why the matrix e wasn't recognised as it was working before.
When I tried it with 3 zeros [a(1,:); 0 0 0;c(:,2).'] . Why won't it accept b as the middle row?
1 个评论
DGM
2023-1-7
Maybe this is some help? I don't know what you're doing with the second row of b, but f and e need to be defined before you can use them.
a = [1 2 3; 4 5 6; 7 8 9]
b = [7 5 6]
c = [19 23; 31 29; 17 13]
% "second row of b" ... but b only has one row?
% e = [a(1,:);b(2,:);c(:,2).'] % matrix e consists of the first row of a, the second row of b and the transpose of the second column of c
e = [a(1,:); b; c(:,2).'] % maybe you meant "the second row is b"?
f = c(:,1).' % matrix f is the transpose of the first column of matrix c
% f and e need to exist first
d = [e(1:2,:); f; e(3,:)] % matrix f is inserted between the third and second row of e creating matrix d
采纳的回答
Image Analyst
2023-1-7
In your code:
a = [1 2 3; 4 5 6; 7 8 9]
b = [7 5 6]
c = [19 23; 31 29; 17 13]
d = [e(1:2,:); f; e(3,:)]; % matrix f is inserted between the third and second row of e creating matrix d
e = [a(1,:);b(1,3);c(:,2).'] % matrix e consists of the first row of a, the second row of b and the transpose of the second column of c
f = c(:,1).' % matrix f is the transpose of the first column of matrix c
m = diag(a(:)) %
m = [diag(a); diag(b)] %
you can see that you're trying to create d by using e. The problem is e has not been created yet. You need to assign a 3-by-3 matrix to e BEFORE you try to use it while creating d.
You can't just swap those lines because you're creating e incorrectly. You have e as a 3 element row vector, a single number, then a 3 elemenet row vector in the bottom row. You can't have the second row be just a single number -- it must be 3 numbers in a row vector.
Then you're also trying to use f while creating d and you have the same problem. f has not yet been defined. You need to define f before you use it while creating d.
Not sure what you want for m but you have the first 3 rows as only a single column, then the bottom 3 rows have 3 columns. You can't have different number of columns when you're stitching arrays together vertically. So perhaps you want one of these two operations, which give different shapes
a = [1 2 3; 4 5 6; 7 8 9]
b = [7 5 6]
c = [19 23; 31 29; 17 13]
e = [a(1,:);b;c(:,2).'] % matrix e consists of the first row of a, the second row of b and the transpose of the second column of c
f = c(:,1).' % matrix f is the transpose of the first column of matrix c
d = [e(1:2,:); f; e(3,:)]; % matrix f is inserted between the third and second row of e creating matrix d
m = diag(a(:)) %
m2 = [diag(a), diag(b)] %
m3 = [diag(a)'; diag(b)] %
m2 =
1 7 0 0
5 0 5 0
9 0 0 6
m3 =
1 5 9
7 0 0
0 5 0
0 0 6
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!