Subscripted assignment dimension mismatch
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello i read examples about passing values into arrays but i cant fix my math problem yet.
this is my programm and gives me Subscripted assignment dimension mismatch error. Please help .
close all;
clear all;
clc;
x=linspace(0,100,1001);
y=linspace(0,30,1001);
z=linspace(0,3,1001);
[A,B,C]=tri_MF_partition(x,0,100,4,0.2);
x_mf=zeros(1,4);
for i=1:4;
x_mf(i,:)=tri_MF(x,A(i),B(i),C(i));
end
figure(1)
plot(x,x_mf(1,:));
plot(x,x_mf(2,:));
plot(x,x_mf(3,:));
plot(x,x_mf(4,:));
------------------------------
here is function codes
tri_MF
function y=tri_MF(x,a,b,c)
if a>b
error('illegal parameter condition:a>b');
elseif b>c
error('illegal parameter condition:b>c');
elseif a>c
error('illegal parameter condition:a>c');
end
y=max(min((x-a)/(b-a),(c-x)/(c-b)),0);
------------------------------------------------------
tri_MF_partition
function [a,b,c]=tri_MF_partition(x,A,B,n,g)
for k=1:n
if k==1
a=A;
b=0;
c=0;
else
a=(A+((B-A)/(n-1)))*((k-1)-1/(2*(1-g)));
end
b=A+(k-1)*((B-A)/(n-1));
if k==n
c=B;
else
c=A+((B-A)/(n-1))*((k-1)+1/(2*(1-g)));
end
0 commentaires
Réponses (4)
Image Analyst
le 9 Mai 2014
A is 79.1666666666667. You can have the 79th element or the 80th element, but you can't have the 79.1666666666667'th element of an array.
1 commentaire
Image Analyst
le 9 Mai 2014
geor, here is the solution to all your problems: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
By the way, when you reply to someone, make a comment on their answer. Don't create an original answer to your original question. When you make an answer like "Sorry but gives ....." and there are 10 people who responded before that, how are we supposed to know whom you're talking to?
geor
le 9 Mai 2014
1 commentaire
dpb
le 9 Mai 2014
Besides that, in
function [a,b,c]=tri_MF_partition(x,A,B,n,g)
...
a) x isn't used; why is it in the argument list?
b) you're missing a closing end on the for loop
c) each iteration thru the loop overwrites the previous values for a, b, and c so in the end the only result returned is for the final iteration of k==n or the last result for than case. The result is that the function might as well be
function [a,b,c]=tri_MF_partition(A,B,n,g)
k=n;
a=(A+((B-A)/(n-1)))*((k-1)-1/(2*(1-g)));
b=A+(k-1)*((B-A)/(n-1));
c=B;
geor
le 9 Mai 2014
1 commentaire
dpb
le 9 Mai 2014
That's a totally different problem--I just pointed out that your function likely isn't doing what you think it should.
I've no idea what your end objective really is so can't really tell you how to fix the above (if it needs fixing) or the answer to the issue raised by IA and the immediate error.
You've got to figure out what it is that you're trying to do or explain it clearly enough folks can decipher incorrect code to the point of having a klew of how to correct it.
The basic problem is that array indexing must be integer-valued; if your objective is such that you are trying to approximate a location to the nearest location in an array perhaps the fix is a simple as using round or fix on the value you've got. If, otoh, the idea is to keep those non-integer values in an array, then you'll have to increment indices of the array as integers and store the values therein. Which is correct is your job as designer. We can help in details of implementation but don't have prescience to know what it is that is the intent behind.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
