Vectorize a simple matrix update operation?

2 vues (au cours des 30 derniers jours)
Grufff
Grufff le 3 Mar 2012
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 commentaires
Jan
Jan le 3 Mar 2012
I'd vote for Oleg's: "T2(2:49,4:-1:1)=T2(2:49,5:8);".
Grufff
Grufff le 3 Mar 2012
Your suggestion works perfectly and is much neater than my attempt, thank you Oleg (and for your vote Jan).
I did actually try something very close to that when I was trying out possibilities myself, but I stupidly forget I needed a negative progression interval to run backwards, and so it gave me an error.
As you asked, I'll mention how I created T2... it's simply a matrix of temperature values, on which I'm running a series of finite central differencing calculations to converge to a steady state value. T2 holds the interim results from calculations using data drawn from T1. Once all calc's are done, T1 is overwritten by T2 and the next iteration commences.
I'm a bit puzzled about why you don't consider this as symmetry. I have an imaginary line running between the nodes in columns 4 and 5, about which temperature values are mirrored symmetrically. So, column 4 is identical to column 5, col 3 with 6 etc etc.
I'd like to be able to accept your answer Oleg but it seems to have been added to the thread as a comment, so the forum doesn't allow me to do that formally (as far as I can see?).
If you'd like to add a quick word as an Answer I'll accept.
Thanks again to you both!

Connectez-vous pour commenter.

Réponse acceptée

Oleg Komarov
Oleg Komarov le 3 Mar 2012
Symmetry: sorry I misread your symmetry vector! It was fine.
The compact version:
T2(2:49,4:-1:1) = T2(2:49,5:8);
My question about how you got T2 and what are you gonna do with a symmetric T2 is motivated by two reasons:
  1. If we see how you got there, maybe we can find a more programmatical way than hardcoding 4:-1:1 and 5:8.
  2. Why would you need to replicate some data that you already have? For plotting reasons it makes sense but otherwise using indexing you can always retrieve it in the order you wish.
Also, by how you got there and what you're gonna do is a question about the coding. Word phrased problems have the advantage of giving you the whole picture, however it is not always portable across disciplines. In my case I may understand financial stuff but not so well heat equations.
  2 commentaires
Grufff
Grufff le 3 Mar 2012
1. I'll post the full script below so you can see what's happening, it's not very long (I'm happy with it now, but I'm sure others could have done a much better job!).
2. The matrix T2 is simply a store for interim calculation results. The calc's are made using numeric values drawn from the "master" matrix T1, but I cannot write them back to T1 immediately because they will influence the remaining calcs in that cycle. I must complete one full iteration without changing the data in T1 part way through.
So, results are written temporarily to T2 and when one full cycle is complete all data from T2 is mapped back onto T1, ready for the next iteration to begin.
It's not strictly necessary for me to correct for all symmetry each time around, only column 4. I could move the others outside of the loop and update them once at the end but it's a trivial matter given the task at hand.
Thank you again Oleg, I have accepted your answer.
Grufff
Grufff le 3 Mar 2012
Full script posted at pastebin, just in case anyone wishes to see it:
http://pastebin.com/0p6gjGe5

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by