Vectorize a simple matrix update operation?
Show older comments
I know this is basic stuff, but I'm still pretty clumsy with Matlab so your patience and advice would be much appreciated.
I'm modelling a simple heat transfer problem and have a symmetry condition around a vertical boundary. I wish to duplicate values from one side of this boundary onto the other, by copying values from one set of matrix columns to another set.
I first did this one column pair at a time:
T2(2:49,1)=T2(2:49,8);
T2(2:49,2)=T2(2:49,7);
T2(2:49,3)=T2(2:49,6);
T2(2:49,4)=T2(2:49,5);
Trying to vectorize this I've moved to:
symmetry_vector=[1 2 3 4 8 7 6 5];
T2(2:49,symmetry_vector(1:4))=T2(2:49,symmetry_vector(5:8));
My results from these two methods are identical, so it seems to work fine, but is there a better way to do this?
Thanks if you can help,
G
3 Comments
Oleg Komarov
on 3 Mar 2012
I think it depends on the general context. How do you create T2 in the first place and what do you do with it afetr the "smmetry". I'am using quotes since yours is not a true symmetry but your repeat a block.
Otherwise it would have been:
T2(2:49,4:-1:1)=T2(2:49,5:8);
Jan
on 3 Mar 2012
I'd vote for Oleg's: "T2(2:49,4:-1:1)=T2(2:49,5:8);".
Grufff
on 3 Mar 2012
Accepted Answer
More Answers (0)
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!