undefined function or variable matlab even the variable excite

1 回表示 (過去 30 日間)
youbaa maa
youbaa maa 2019 年 2 月 7 日
コメント済み: Rena Berman 2019 年 3 月 5 日
i have this function but when I simulate the function I got a problem
I am receiving the following error message,Undefined function or variable A,how can I resolve this issue?
this is the message
Undefined function or variable 'A'.
Error in T (line 21)
df=(sum(sum(A))^-1);
the function
function [teta]=T(X)
global n m
p1=1;
p2=2;
t=1;
for i=2:n
for j=2:m
k=1;
vect=zeros(4,1);
for h=0:p1
for l=0:p2
vect(k)=X(i-h,j-l);
k=k+1;
end
end
f(t,:)= vect(2:4)
A(t)= vect(2:4)'*vect(2:4)
t=t+1;
end
end
df=(sum(sum(A))^-1);
t=0;
for i=2:n
for j=2:m
t=t+1;
g(t,:)=f(t,:)*X(i,j);
end
end
teta=df*sum(g);
end
the main programme
C=imread('D1.gif');
X=im2double(C);
[m,n]=size(X);
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 2 月 7 日
this user edited away a question with the same title, so people may wish to consider whether it is worth replying.
Rena Berman
Rena Berman 2019 年 3 月 5 日
(Answers Dev) Restored edit

サインインしてコメントする。

回答 (1 件)

Guillaume
Guillaume 2019 年 2 月 7 日
編集済み: Guillaume 2019 年 2 月 7 日
A will only exist if both n and m are greater than 2. So if matlab tells you that A does not exist we can safely assume that either n or m (or both) is smaller than 2. I suspect that they're both 0 since, at least in the code you show, you never give them a value.
When a variable is global, it must be declared global in every function/script that use them. However, as we keep on saying, don't use global variables. They lead to the exact type of problems you're now facing, they're not the value you expect them to be because tracking what happens to these variables is difficult. In your particular case, there's absolutely no need to have them global, they could just be inputs to the function, just as X is. And actually, since m and n are just the size of X and X is an input, they're not even needed. The function could just start with:
[m, n] = size(X);
edit: Note that after you've fixed that problem, the next error you'll get is Array indices must be positive integers or logical values. When j = 2 and l = 2 you'll be indexing x(i-h, 0) which is of course invalid.
A few notes about your code:
  • Write comments that explain what it does. Also write comments that explains what the inputs and outputs of your function are (expected types, sizes, etc.)
  • Use meaningful variable names. It's very easy to get confused over which variable is what when they're all single letters
  • Avoid convoluted expressions x^-1 is normally written as 1/x. columnvector'*columnvector is clearer as sum(vector.^2). The latter has the advantage to work regardless of the shape of the vector.
  • Why create a 6 elements vector if you only use 3 of them?
  • As far as I can tell, none of the loops are needed.
edit edit: I see that you were shown how to pass m and n to your function in your previous question. So, if you're willing to listen to what you're told it's no wonder you run into problems. And of course, if you edit away the question once you get an answer, very quickly you'll run out of people willing to help you.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by