Extract every second value of a vector and write into a new vector
Show older comments
Hi, another trivial question i guess - i'm a beginner at Matlab.
MATLAB code
Xi = sym('Xi',[1,n2]);
for i=1:1:6
BGx(i,1)=L1*Xi(i);
BGy(i,1)=L5*Xi(i);
end
I want to write the results into a new vector. I tried vertcat and some for loops but it didn't work out.
MATLAB code
BG=vertcat(BGx; BGy);
Here i get a result like (L1*Xi1, L1*Xi2, L1*Xi3, ...., L5*Xi1, ...., L5*Xi6) but i want to get (L1*Xi1, L5*Xi1, L1*Xi2, L5*Xi2, ...., L5*Xi6). Is there a trick when using vertcat or do i need to write a loop. I have the problem that BG has another dimension than the 2 vectors BGx and BGy. Thanks in advance
3 Comments
Stephen23
on 3 Feb 2017
@Grillteller: is there a particular reason why you need to use sym? If not, then you are learning some incredibly inefficient and difficult ways to write MATLAB code. Why not simply use fast and efficient array operations with vectorized code?
Grillteller
on 6 Feb 2017
Stephen23
on 6 Feb 2017
Accepted Answer
More Answers (1)
This is MATLAB so why use an ugly loop? MATLAB is much better than that! The code is simpler without it (I also got rid of sym: you don't say why you need it, and numeric operations will be much faster and more efficient):
>> X = [1,2,3,4];
>> L1 = 90;
>> L5 = 10;
>> Z = [L1+X;L5+X];
>> Z = Z(:)'
Z =
91 11 92 12 93 13 94 14
Categories
Find more on Formula Manipulation and Simplification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!