Need help with: How to fill a matrix with vectors as elements? (Error: ??? Subscripted assignment dimension mismatch.)

3 views (last 30 days)
Hello, as in the title, I am trying to form an array composed of vectors as elements.
In my code I have 3 arrays:
jc is a 3xj matrix, jn is nx2 matrix and m is a nx2 matrix.
(Actually I'm trying to simulate a truss structure of j joints and n members where jc is the matrix containing the coordinates of the joints, jn showing which member is composed of which joints and finally the matrix m showing the coordinates of end points of each member hence showing the coordinates of the joints the member is composed of.)
Here is my code:
jc = xlsread('jc.xlsx') ; jn = xlsread('jn.xlsx');
n = size(jn,1) ; m = zeros(n,2);
for i = 1:n;
m(i,1) = [ jc(:,jn(i,1)) ];
m(i,2) = [ jc(:,jn(i,2)) ];
end
%plot3(jc(1,:),jc(2,:),jc(3,:),'s','MarkerSize',3,'MarkerFaceColor','b')
%plot3(m(:,1),m(:,2),m(:,3))
And this is the error I get:
??? Subscripted assignment dimension mismatch.
Error in ==> aee206demircan at 4
m(i,1) = [ jc(:,jn(i,1)) ];
How can I solve this problem?
(The first plot is to show the joints and the second one is to show the members.)

Answers (1)

Walter Roberson
Walter Roberson on 17 Apr 2014
Vectors cannot be elements of a numeric matrix. Vectors can be elements of a cell array.
jc = xlsread('jc.xlsx') ; jn = xlsread('jn.xlsx');
n = size(jn,1) ; m = cell(n,2);
for i = 1:n;
m{i,1} = [ jc(:,jn(i,1)) ];
m{i,2} = [ jc(:,jn(i,2)) ];
end
Now m{1,1}, m{1,2}, m{2,1}, m{2,2} and so on exist and are vectors. Notice the use of {} instead of ().
%plot3(m(:,1),m(:,2),m(:,3))
You do not have any m(:,3) in your code.
Your intent of your commented-out plot3() statement is not clear. Remember that you stored vectors into each element of m, so what you intend by m(:,1) or m(:,2) is not clear.
What I speculate might work for you is
n = size(jn,1) ; m = cell(3,n);
for i = 1:n;
m{1,i} = jc(:,jn(i,1));
m{2,i} = jc(:,jn(i,2));
m{3,i} = jc(:,jn(i,3));
end
plot3(m{:})
  2 Comments
Osman Mirza Demircan
Osman Mirza Demircan on 18 Apr 2014
Edited: Osman Mirza Demircan on 18 Apr 2014
Thank you sir, it helped a lot. Here's the working code:
jc = xlsread('jc.xlsx') ; jn = xlsread('jn.xlsx');
n = size(jn,1) ; m = cell(n,2);
for i = 1:n;
m{i,1} = [ jc(:,jn(i,1)) ];
m{i,2} = [ jc(:,jn(i,2)) ];
end
subplot(1,2,1)
plot3(jc(1,:),jc(2,:),jc(3,:),'s','MarkerSize',3,'MarkerFaceColor','b')
subplot(1,2,2)
plot3(m{:,1},m{:,2})
Still I was expecting 3 members to be shown on the plot but there's only two...
Walter Roberson
Walter Roberson on 18 Apr 2014
plot3(m{:,1},m{:,2}) is the same as writing
plot3{m{1,1}, m{2,1}, m{3,1}, ...., m{end,1}, m{1,2}, m{2,2}, m{3,2}, .... m{end,2})
that's probably not going to do you much good.
Notice in my speculative code, I pulled out three vectors per item, and I reversed the order of the indices, and I used m{:} in the plot3(). The result is like writing plot3(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) where yours was more like writing plot3(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!