Info

此问题已关闭。 请重新打开它进行编辑或回答。

Subscripted assignment dimension mismatch.

1 次查看(过去 30 天)
soloby
soloby 2015-6-23
关闭: MATLAB Answer Bot 2021-8-20
x = -10:0.1:10;
lower_1 = zeros(numel(x), 2);
upper_1 = zeros(numel(x), 2);
f1 = trapmf(x,[-2 0 0 2]);
[ss, firstmax] = max(f1);
lower_1(1:firstmax, :) = [x(1:firstmax), f1(1:firstmax)];
upper_1(firstmax:end, :) = [x(firstmax:end, :), f1(firstmax:end, :)];
I get a subscripted assignment dimesnion mismatch error. Why is this??
  1 个评论
soloby
soloby 2015-6-23
when i type in [x(1:firstmax), f1(1:firstmax)] in matlab, i get a 1x202 matrix for some reason?? should i not be getting a 2xlength(firstmax) matrix?

回答(2 个)

Ingrid
Ingrid 2015-6-23
you should do this
lower_1(1:firstmax, :) = [x(1:firstmax)',f1(1:firstmax)'];
upper_1(firstmax:end,:) = [x(firstmax:end)', f1(firstmax:end)'];
since your x is a rowvector and not a columnvector or transpose x first
  3 个评论
cwshep
cwshep 2015-6-23
It is because firstmax:end is empty (since firstmax is greater than end). To do it this way it would need to be: "upper_1(firstmax:end, :) = [x(:,firstmax:end).', f1(:, firstmax:end).'];"
Note that it's good habit to do a .' than just a ', since if they are complex values the ' does a conjugate transpose.
Ingrid
Ingrid 2015-6-23
编辑:Ingrid 2015-6-23
look at what I have shown in the code above you should not index x as x(firstmax:end,:) because this will be empty since x has dimensions 1x201 so just leave out the last :
but even easier would be to FIRST transpose x as I have also commented and then f1 is also a column vector and all subsequent transposes are not required which makes your code easier to maintain

cwshep
cwshep 2015-6-23
编辑:cwshep 2015-6-23
Your x and f1 arrays are 1x201. Your upper_1 array is 201x2. Presumably you want to do a "x = x.'; f1 = f1.';" before your assignment to lower_1.
Actually, the cleanest and best way is probably to just change your first line to "x = [-10:0.1:10].';"

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by