Vectorize a simple matrix update operation?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Oleg Komarov
2012년 3월 3일
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:
- If we see how you got there, maybe we can find a more programmatical way than hardcoding 4:-1:1 and 5:8.
- 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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!