Array operations- vectorization. Error with matrix dimensions.

Hi! Sorry if it's stupid question, but I'm newbie with MATLAB. I wanted to use vectorization in my function which applies forward mapping (affine transformation of 2D image). I found vectorization in MATLAB documentation https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html?requestedDomain=true#btjk57v-1 . The problem is I wanted to try the example stated there under the topic of array operations.
x = (-2:0.2:2)'; % 21-by-1
y = -1.5:0.2:1.5; % 1-by-16
F = x.*exp(-x.^2-y.^2); % 21-by-16
And the error appeared: Error using - Matrix dimensions must agree. What can be a problem here? It is example from documentation, so I'm kinda confused why it is not working for me.

 채택된 답변

Guillaume
Guillaume 2018년 1월 15일
The problem is simple, you're looking at the most recent documentation but you're using an old version of matlab, earlier than R2016b.
From matlab's command prompt type
web(fullfile(docroot, 'matlab/matlab_prog/vectorization.html'))
to see the documentation that apply to your version.
R2016b introduced implicit expansion, in earlier versions you have to use bsxfun to do the same. The example code would have to be:
x = (-2:0.2:2)'; % 21-by-1
y = -1.5:0.2:1.5; % 1-by-16
F = bsxfun(@times, x, exp(bsxfun(@plus, -x.^2, -y.^2)))

댓글 수: 2

Yea, I supposed that this can be the problem with my version of MATLAB (2015b from the moment I bought it). I kept forgetting to update it. So far I didn't have any problems. I'll try both meshgrid and bsxfun and check the results. Thanks for clarification :)
Using bsxfun should be faster and definitively will use less memory than meshgrid. bsxfun is the direct ancestor of the implicit expansion that is used in the example.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Torsten
Torsten 2018년 1월 15일

0 개 추천

https://de.mathworks.com/help/matlab/ref/meshgrid.html
Best wishes
Torsten.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 1월 15일

댓글:

2018년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by