Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i need to concatnat a vector in matlab and i dont know how

I want to concatenate two vectors in MATLAB, but I don’t know the correct way to do it. My goal is to create a single vector that combines all the elements of both vectors in the order they are defined.

Here’s what I tried so far:
v1=[1 2 3] v2=[4 5 6] v3=v1 +v2

but the result is v3=[5 7 9] but i wanted v3=[1 2 3 4 5 6]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In MATLAB, the + operator is used for element-wise addition, which is why you’re getting the result [5 7 9]. To concatenate two vectors, you should use square brackets ([]), not the + operator.

Correct way to concatenate vectors:
matlab
Copy code

v1 = [1 2 3];
v2 = [4 5 6];
v3 = [v1 v2];  % Concatenate the vectors

Result:
matlab
Copy code

v3 = [1 2 3 4 5 6]

This will give you the desired result: a single vector that combines all the elements of both v1 and v2 in the order they are defined.

Explanation:
[v1 v2] concatenates the two row vectors v1 and v2 side by side (horizontally). If you wanted to concatenate them vertically, you would use a semicolon between the vectors like this:
matlab
Copy code

v3 = [v1; v2];  % Concatenate vertically (creates a 2x3 matrix)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading