Unrecognized function or variable 'e'

5 vues (au cours des 30 derniers jours)
Natalie
Natalie le 7 Jan 2023
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 commentaire
DGM
DGM le 7 Jan 2023
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]
a = 3×3
1 2 3 4 5 6 7 8 9
b = [7 5 6]
b = 1×3
7 5 6
c = [19 23; 31 29; 17 13]
c = 3×2
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"?
e = 3×3
1 2 3 7 5 6 23 29 13
f = c(:,1).' % matrix f is the transpose of the first column of matrix c
f = 1×3
19 31 17
% 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
d = 4×3
1 2 3 7 5 6 19 31 17 23 29 13

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 7 Jan 2023
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

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by