MATLAB Answers

Two vectors in just ONE but in alternation

1 view (last 30 days)
Tay
Tay on 26 May 2020
Commented: Tay on 27 May 2020
Hi all !!
I have, maybe, simple question but this is getting me. I'm trying to save two vector in just one (I need everything compact in one). But the thing is that I need the values of those vector to be in alternation. So my code is:
for a = 1:step
fHj(a) = ((E(a)^2 - nL^2)/(nH^2 - nL^2))*fTj;
fLj(a) = fTj - fHj(a);
end
The vectors fHj and fLj have to be in just one vec but in alternation, so vec must be vec = (fHj(1),fLj(1),fHj(2), fLj(2),fHj(3), fLj(3), ... and soon).Is it even possible?
I really appraciate any help !!
Best regards,
Tay

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 May 2020
Edited: Image Analyst on 26 May 2020
Try this:
v1 = 1:10
v2 = 11:20
v=[v1(:), v2(:)]
vec = reshape(v', 1, [])
v =
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
vec =
1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20

  5 Comments

Show 2 older comments
Image Analyst
Image Analyst on 27 May 2020
Great minds think alike. Sometimes I use more lines than is necessary because for some (maybe even most, hopefully) beginners, it helps them if they see the intermediate results rather than just the final result alone - helps them understand what is going on. And usually I'll add comments which makes it even longer but explains it better. With other beginners though, it seems like they skip my usually longer solution and just go with the solution with fewest lines possible, even if it is more cryptic and hard to follow (not that yours was though) - evidently those people are just more interested in short code and the right answer rather than understanding what's going on. So my philosophy is to bias on the side of understandability, even if it costs me some accepted answers.
Adam Danz
Adam Danz on 27 May 2020
...and you still passed 10,000 accepted answers today :)
Tay
Tay on 27 May 2020
And I accepted yours because I could see the results and understand the logical behind. I'm beginner as you said because literally I'm working with Matlab everday this year. And, of course, a lot of functions I don't know but that is always a good person that explain and help :)) So thanks to do that.

Sign in to comment.

More Answers (0)

Categories