splitting up a graph into 2 parts
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
Respuestas (1)
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
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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!