splitting up a graph into 2 parts

6 次查看(过去 30 天)
soloby
soloby 2015-6-23
评论: Guillaume 2015-6-23
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 个评论
soloby
soloby 2015-6-23
ix is the iteration of my maximum value which is 1
note: [a,ix]=max(f1)

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2015-6-23
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 个评论
soloby
soloby 2015-6-23
编辑:Guillaume 2015-6-23
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 2015-6-23
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by