splitting up a graph into 2 parts

I'm trying to divide my triangular wave f1 into 2 parts, left and right of the upper point
x = -10:0.1:10;
lower_1 = 0:.005:1;
upper_1 = 0:.005:1;
y2 = 0:.005:1;
f1 = trapmf(x,[-2 0 0 2]);
[a,ix]=max(f1);
for k = 1:ix
lower_1(k,:) = [x(k), f1(k)];
end
for k1 = ix:201
upper_1(k1,:) = [x(k1), f1(k1)];
end
this seems correct to me, but it's giving me a dimension error in the second for loop.
any ideas why?

2 comentarios

Purushottama Rao
Purushottama Rao el 23 de Jun. de 2015
what is ix in your code?
soloby
soloby el 23 de Jun. de 2015
ix is the iteration of my maximum value which is 1
note: [a,ix]=max(f1)

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 23 de Jun. de 2015
I would think you get the error in the first loop. You're trying to put two values, the vector [x(k) f1(k)] into a scalar lower_1(k) (note that the , : in your expression has no effect since lower_1 is a column vector. lower_1(k, :) is the same as lower_1(k, 1) is the same as lower_1(k)).
Whatever you're trying to do, you don't need a loop anyway. If you're trying to create an nx2 matrix of x and f1 split at the max:
x = -10:0.1:10;
lower_1 = zeros(numel(x), 2);
upper_1 = zeros(numel(x), 2);
f1 = trapmf(x,[-2 0 0 2]);
[~, firstmax] = max(f1);
lower_1(1:firstmax, :) = [x(1:firstmax), f1(1:firstmax)];
upper_1(firstmax:end, :) = [x(firstmax:end, :), f1(firstmax:end, :)];
Note that the above assumes that trapmf returns a column vector (I don't have the fuzzy logic toolbox so can't check).
Also note, that nowhere have I hardcoded the size of the vectors (201). I use numel or end, so if it ever changes, there's nothing to change in the code.

4 comentarios

soloby
soloby el 23 de Jun. de 2015
I actually get through the first loop just fine but get stuck on the second, but would love to do it without using a loop.
you're right using your code, f1 would be 1x201 but lower_1 and upper_1 are both 201x2, gives me a dimension error. would using something like an inverse be the solution to this?
Guillaume
Guillaume el 23 de Jun. de 2015
if f1 is a row vector (1x201), then you just need to transpose it:
f1 = trapmf(x, [-2 0 0 2]).'; %note the .' to transpose
soloby
soloby el 23 de Jun. de 2015
Editada: Guillaume el 23 de Jun. de 2015
x = -10:0.1:10;
lower_1 = zeros(numel(x), 2);
upper_1 = zeros(numel(x), 2);
f1 = trapmf(x,[-2 0 0 2]).';
[~, firstmax] = max(f1);
lower_1(1:firstmax, :) = [x(1:firstmax), f1(1:firstmax)];
upper_1(firstmax:end, :) = [x(firstmax:end, :), f1(firstmax:end, :)];
still giving me an error
"Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in (line 6)
lower_1(1:firstmax, :) = [x(1:firstmax), f1(1:firstmax)];"
did you mean to change f1 to its transverse in the variable statement or the coding?
Guillaume
Guillaume el 23 de Jun. de 2015
Please, use the code formatting button ( {}Code) rather than putting spaces between lines of code.
Sorry, for some reason I thought that x was a column vector but it's a row vector. Depending on what you want, x first column, f second column or x first row, f second row, you can either transpose x and leave the rest as is, or not transpose f, swap the dimensions of lower and upper and concatenate x and f verticually:
%|x| and |f| as column vectors:
x = (-10:0.1:10)';
%... rest of the code as above
%|x| and |f| as row vectors
x = -10:0.1:10;
lower = zeros(2, numel(x));
upper = zeros(2, numel(x));
f = trapmf(x, [-2 0 0 2]);
[~, firstmax] = max(f1);
lower(1:firstmax) = [x(1:firstmax); f(1:firstmax)]; %note the ; instead of , for concatenation.

Iniciar sesión para comentar.

Categorías

Más información sobre Fuzzy Logic in Simulink en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Jun. de 2015

Comentada:

el 23 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by