Undefined Function or Variable
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to create a function at the end of my script. I believe in my function I defined the variable ‘u.’ But then later down in the function when I try to use ‘u’ I get an error saying that ‘u’ is not defined. I am attaching an image with my code and the error. Let me know if anyone has any suggestions. Any help is appreciated.
For some reason I am having trouble submitting multiple images but I have two ‘end’s at the end of my code. Also the error message is in line 272 ‘if u==1’
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285619/image.jpg)
댓글 수: 0
채택된 답변
Tommy
2020년 4월 18일
length(o) and length(n) will both be 1, as o and n are scalars, so your code never enters your for loops. You should instead use just o and n in their place. i.e.
for W = 1:o-1
댓글 수: 5
Tommy
2020년 4월 19일
Always happy to help! Don't sweat it.
"I don't know if maybe in my u(W)=1/2/0 if I need to do u(W,K) or something like that."
If you do want to test this for all columns, then you would need a second index. Otherwise, the results of each column are being overwritten by the results of the next column. Check if u contains the expected output for the very last column in A - I believe that should be the case...
Also, are any values within A negative?
If I am understanding correctly, the point of this function is to tell the user whether values in A increase or decrease as you move down rows? Can one column increase while another column decreases?
You could simplify the process by using diff and sign. Consider these examples:
>> A = [10 20 30;40 50 60;70 80 90]
A =
10 20 30
40 50 60
70 80 90
>> sign(diff(A))
ans =
1 1 1
1 1 1
>> A = [70 80 90;40 50 60;10 20 30]
A =
70 80 90
40 50 60
10 20 30
>> sign(diff(A))
ans =
-1 -1 -1
-1 -1 -1
>> A = [10 20 30;70 80 90;40 50 60]
A =
10 20 30
70 80 90
40 50 60
>> sign(diff(A))
ans =
1 1 1
-1 -1 -1
>> A = [10 80 60;70 50 90;40 20 30]
A =
10 80 60
70 50 90
40 20 30
>> sign(diff(A))
ans =
1 -1 1
-1 -1 -1
Then it seems to me that you'd want to check if every row is increasing across every column. You might then use all. Am I on the right track?
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!