Getting an error "Matrix dimensions must agree" in my matlab program.help

N(1)=0.5*(epsilon3*epsilon3-epsilon3); %%epsilon=natural coordinte
N(2)=(1-epsilon3*epsilon3);
N(3)=0.5*(epsilon3*epsilon3+epsilon3);
N_matrix = zeros(9, 27);
for i=1:9
N_matrix(3,1:9)=repmat(N, 3, 1) .* repelem(ez, 3, 1)(ERROR HERE)
end
Error using .* Matrix dimensions must agree.
Error in practisee2 (line 232) N_matrix(3,1:9)=repmat(N, 3, 1) .* repelem(ez, 3, 1)

Answers (2)

repmat(N,3,1) has three elements, repelem(ez,3,1) has four elements.
So matrix dimensions don't agree.
Best wishes
Torsten.

1 Comment

Perhaps -- but the code appears to be derived from an different question in which Jan's suggestion with the for loop was intended to run with a column vector of N, not a row vector.

Sign in to comment.

We do not know what size ez is?
We do not know if you have pre-defined N to be something other what is implied by this code?
This code implies N becomes 1 x 3: when you do not initialize a variable but assign multiple entries to it using linear indexing, then it makes a row vector.
repmat(N, 3, 1) would be 3 x 3. If the repelem(ez,3,1) were compatible for .* then the result would have to be 3 x 3. But you are trying to store that 3 x 3 into a 1 x 9 slot.
I speculate that your expectation was that assigning consecutive linear indices to a variable resulted in a column vector.

5 Comments

ex = [lx; mx; nx] / J;
ey = [ly; my; ny] / lve;
ez(1)=ex(2)*ey(3)-ex(3)*ey(2);
ez(2)=ex(1)*ey(3)-ex(3)*ey(1);
ez(3)=ex(1)*ey(2)-ex(2)*ey(1);
Those lines would make your ex and ey vectors into column vectors (unless J or lve are not scalars). It would, however, make ez into a row vector unless it had been initialized beforehand.
You should check size(N) and size(ez) . If they are the same then repmat(N, 3, 1) .* repelem(ez, 3, 1) should work. If both of them are the same length but different orientation then the .* would be an error up to R2016a but would work starting from R2016b. If both of them are row vectors of length 3 then the result of the .* would be 3 x 3 but that would be the wrong shape to fit into the destination.
will repmat(N, 3, 1) .* repelem(ez, 3, 1) give the answer that i am expecting because i read about repmat and repelem. They were about repeating the value of an array. Is that related to my problem??
repmat() of a 3 x 1 with count 3, 1, is going to give a 9 x 1 vector.
repelem() of a 1 x 3 with count 3, 1, is going to give a 3 x 3 array.
It is not valid to .* between a 9 x 1 and a 3 x 3.
Try
N_matrix(3,1:9) = (repmat(N(:), 3, 1) .* repelem(ez(:), 3, 1)).';

Sign in to comment.

Categories

Tags

Asked:

on 18 Oct 2018

Commented:

on 18 Oct 2018

Community Treasure Hunt

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

Start Hunting!