Asked by Noru
on 21 Mar 2013

I have some trouble when creating matrix 4x2 with rules like below. the first column are integer and the other column are random number. I already create the syntax below.

for ii = 1:8, nilai = mod(ii,2); if nilai == 0 x(ii) = 0.95 + (1.05-0.95)*x(ii); elseif nilai ~= 0 x(ii) = round(3 + (30-3)*x(ii)); elseif nilai ~= 0 x(ii) = 0.01 + (0.055-0.01)*x(ii); else x(ii) = 1.0 + (1.15-1.0)*x(ii); end end x = reshape(x,4,2)';

but the result are

??? Error using ==> reshape To RESHAPE the number of elements must not change.

anyone can help me with this problem..? any solutions are very welcome..

thank you.

*No products are associated with this question.*

Answer by the cyclist
on 21 Mar 2013

Accepted answer

What is the shape of x before you start? It seems that it is probably not an 8-element array, so the reshape fails.

the cyclist
on 21 Mar 2013

You did not answer my question. What is the shape of x BEFORE this loop?

As you can see from Image Analyst's answer, there is no problem if x is 1x8 before your loop.

Answer by Image Analyst
on 21 Mar 2013

I got a different error message due to x not being predefined and your new x(ii) depends on your old x(ii). When I predefined x, it ran with no errors:

x = zeros(1, 8); for ii = 1:8 nilai = mod(ii,2); if nilai == 0 x(ii) = 0.95 + (1.05-0.95)*x(ii); elseif nilai ~= 0 x(ii) = round(3 + (30-3)*x(ii)); elseif nilai ~= 0 x(ii) = 0.01 + (0.055-0.01)*x(ii); else x(ii) = 1.0 + (1.15-1.0)*x(ii); end end x = reshape(x,4,2)'

In the command window:

x = 3 0.95 3 0.95 3 0.95 3 0.95

Image Analyst
on 21 Mar 2013

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments