This question is closed. Reopen it to edit or answer.

Subscripted assignment dimension mismatch.

1 view (last 30 days)
soloby on 23 Jun 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
soloby on 23 Jun 2015
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?

Answers (2)

Ingrid on 23 Jun 2015
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
cwshep on 23 Jun 2015
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 on 23 Jun 2015
Edited: Ingrid on 23 Jun 2015
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 on 23 Jun 2015
Edited: cwshep on 23 Jun 2015
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!