Inserting elements of one matrix into another not working?

Hi, all the variables below i.e. x1, x2, x3, y1, y2, y3 are ARRAYS of the same dimensions. But for some reason, I am not able to concatenate them, and get the horzcat error?
A= [x1 y1 1,
x2 y2 1,
x3, y3, 1];
B=-[x1.^2+y1.^2 y1 1,
x2.^2+y2.^2 y2 1,
x3.^2+y3.^2 y3 1]
C=[x1.^2+y1.^2 x1 1,
x2.^2+y2.^2 x2 1,
x3.^2+y3.^2 x3 1]
D=-[x1.^2+y1.^2 x1 y1,
x2.^2+y2.^2 x2 y2,
x3.^2+y3.^2 x3 y3];

Answers (1)

"But for some reason, I am not able to concatenate them,"
You are trying to concatenate arrays of two difference sizes: the first being sizes of x1, etc, the second being scalar arrays of ones. Have a look at your code:
A = [x1 y1 1,
x2 y2 1,
x3, y3, 1];
What size is x1 ? What size is 1 ? Do they have the same number of rows? Unless x1 is a scalar or a row vector, then they will have a different number of rows.
Solution One: Given that all of x1, etc have the same size then you can replace all of the scalar 1's with this:
tmp = ones(size(x1,1),1);
e.g.:
A = [x1,y1,tmp;
x2,y2,tmp;
x3,y3,tmp];
Solution Two: First create the matrix without ones, then add the ones:
A = [x1,y1;
x2,y2;
x3,y3];
A(:,end+1) = 1;
Note that in both cases I also corrected your inconsistent commas, and added the semi-colons to indicate the end of the row.

6 Comments

I have just modified it, but it is still not working. I believe I need to use some sort of iterative function.
x1, x2, x3, y1, y2, y3, and I are all 1925x1 arrays.
x1 = X_Coord(1:3:5775,:)
x2 = X_Coord(2:3:5775,:)
x3 = X_Coord(3:3:5775,:)
y1 = Y_Coord(1:3:5775,:)
y2 = Y_Coord(2:3:5775,:)
y3 = Y_Coord(3:3:5775,:)
Z=x1.^2+y1.^2
Y=x2.^2+y2.^2
X=x3.^2+y3.^2
I(1:1925,1)=1
A= [x1 y1 I,
x2 y2 I,
x3 y3 I];
B=-[Z y1 I,
Y y2 I,
X y3 I]
C=[Z x1 1,
Y x2 1,
X x3 1]
D=-[Z x1 y1,
Y x2 y2,
X x3 y3];
"I believe I need to use some sort of iterative function."
Why do you need something iterative? What would that achieve?
How about we actually try the two solution I gave in my answer, and see if they work:
>> x1 = rand(1925,1);
>> x2 = rand(1925,1);
>> x3 = rand(1925,1);
>> y1 = rand(1925,1);
>> y2 = rand(1925,1);
>> y3 = rand(1925,1);
Solution One:
>> tmp = ones(size(x1,1),1);
>> A = [x1,y1,tmp;
x2,y2,tmp;
x3,y3,tmp];
No error!
Solution two
>> B = [x1,y1;x2,y2;x3,y3];
>> B(:,end+1) = 1;
No error!
Do they give the same output? Lets check:
>> isequal(A,B)
ans = 1
So I tried both solutions that I gave in my answer and they both worked without error. If you are getting any error message then you need to give the complete error in a comment. Simply stating that "it is still not working" tells me nothing useful at all. If you show the error message then we can figure out why it is not working.
Thanks Stephen, I realized my mistake, I framed my question wrong:
I need to pre-allocate values from each individual matrix into the new one. i.e. x1(1,1),x2(1,1),x3(1,1)..etc etc must be inserted in A, and that is one matrix.
And then x1(2,1),x2(2,1),x3(2,1)...into A again.
So on and so forth creating a new matrix each time.
If you could help me on that, it would be greatly appreciated.
"I need to pre-allocate values from each individual matrix into the new one. i.e. x1(1,1),x2(1,1),x3(1,1)..etc etc must be inserted in A, and that is one matrix."
Why do you need to "preallocate"? With the methods I have shown you, all of the elements of x1, x2, etc get included in the matrix A all at once. With MATLAB there is no need to do things one-element-at-a-time, because operating on complete arrays is much simpler and more efficient.
"So on and so forth creating a new matrix each time."
Magically creating arrays in a loop is possible, but is complex, slow, and very buggy code. Read this to know more:
In any case, it is not clear to me now what you are actually trying to do. Your original question was about concatenating matrices together. I showed you how to do that. Now you are talking about "inserting" values into arrays, but it is unclear what you expect to happen, or how this relates to your original question.
Please clarify, and tell us what you are trying to do.
X and y are coordinates of a race track, and I am trying to find the radius of curvature using 3 coordinates at a time:
This link has the method I am using:
So I split my original XY matrices using intervals of 3 as follows:
x1 = X_Coord(1:3:5775,:) x2 = X_Coord(2:3:5775,:) x3 = X_Coord(3:3:5775,:) y1 = Y_Coord(1:3:5775,:) y2 = Y_Coord(2:3:5775,:) y3 = Y_Coord(3:3:5775,:)
and then i tried using this code to find the radius of curvatures:
Z=x1.^2+y1.^2 Y=x2.^2+y2.^2 X=x3.^2+y3.^2
I(1:1925,1)=1
A= [x1 y1 I, x2 y2 I, x3, y3, I];
B=-[Z y1 I, Y y2 I, X y3 I]
C=[Z x1 I, Y x2 I, X x3 I]
D=-[Z x1 y1, Y x2 y2, X x3 y3];
Det_A=det(A); Det_B=det(B); Det_C=det(C); Det_D=det(D);
rho==sqrt(((Det_B.^2+Det_C.^2)-(4.*Det_A.*Det_D))./(4.*Det_A.^2))
@MathGeek: your code will not work, because as that page explains, those matrices need to be solved as determinants. The page also gives the solutions once they have been solved, and you can simply use those solved equations. I even put them into a function for you:
function r = pts2rad(x1,y1,x2,y2,x3,y3)
t1 = x1.^2 + y1.^2;
t2 = x2.^2 + y2.^2;
t3 = x3.^2 + y3.^2;
A = x1.*(y2-y3) - y1.*(x2-x3) + x2.*y3 - x3.*y2;
B = t1.*(y3-y2) + t2.*(y1-y3) + t3.*(y2-y1);
C = t1.*(x2-x3) + t2.*(x3-x1) + t3.*(x1-x2);
D = t1.*(x3.*y2-x2.*y3) + t2.*(x1.*y3-x3.*y1) + t3.*(x2.*y1-x1.*y2);
r = sqrt((B.^2+C.^2-4*A.*D)./(4*A.^2));
end
And tested it (using the values from the page):
>> pts2rad(-3,4,4,5,1,-4)
ans = 5
The function will work with any size inputs, so give it a try with your vectors.

Sign in to comment.

Asked:

on 22 Oct 2017

Edited:

on 22 Oct 2017

Community Treasure Hunt

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

Start Hunting!