Clear Filters
Clear Filters

Reshape won't work for my application. Why?

1 view (last 30 days)
Zach Dunagan
Zach Dunagan on 8 Dec 2017
Commented: Zach Dunagan on 8 Dec 2017
Converting a line of Python code to matlab.
Here is the Python.
Fy_bern=np.sum(np.reshape(forceDist,(numPanels,1))*(np.sin(theta_t[t])*np.sin(theta)+np.cos(theta_t[t])*np.cos(theta)))
The matlab
Fy_bern = sum(reshape(forceDist, [numPanels, 1]) .* (sin(theta_t(t)) .* sin(theta) + cos(theta_t(t)) .* cos(theta)));
The error I get is Error using reshape To RESHAPE the number of elements must not change.
forceDist is a 32x30 from my understanding about reshape is it takes your array and puts it in a nxm matrix. I even abstracted reshape(forceDist, [numPanels, 1]) and ran it. Still gives me the same error.
reshape(forceDist, [numPanels, 1])

Answers (1)

John D'Errico
John D'Errico on 8 Dec 2017
Reshape will NOT change the number of elements. Regardless of what something named reshape does in Python, MATLAB is not Python.
You need either to discard some of your elements or pad it sufficiently such that the number of elements is correct.
If this is behavior that you want to see, then nothing stops you from writing code that behaves as you wish. But reshape in MATLAB will not do what it is not programed to do.
  1 Comment
Zach Dunagan
Zach Dunagan on 8 Dec 2017
I actually found a bug in the code which was preventing reshaping from working... Oops.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!