From: Nick on 28 May 2010 10:56 I have the following info: ID ImportantNumb 1 100 2 300 3 200 OldID NewID 1 4 2 5 3 8 4 6 5 7 6 9 7 10 I'd like to move ImportantNumb to the appropriate line matching NewID with OldID. So I'd end up with: ID ImportantNumb 1 100 2 300 3 200 4 100 5 300 6 100 7 300 8 200 9 100 10 300 Right now I'm doing that by first making a matrix: MatrixGuy: 1 100 2 300 3 200 4 0 5 0 8 0 6 0 7 0 9 0 10 0 and using this bit of code: while sum(MatrixGuy(:,2)==0)>0 [~,b,c]=intersect(MatrixGuy(:,1),OldID); MatrixG(3+c,2)=MatrixG(b,1); end (the 3 isn't actually hard-coded in but you get the point) My question is: Is there any way to do this without looping? The real data is much bigger and the time spent looping is killing my program. Is the only solution to use a mex file in this case? I just wanted to check here before I went down that path, any help is much appreciated.
From: Matt Fig on 28 May 2010 11:56 The code you gave won't run because MatrixG is undefined. I do not see why 4 is matched with 100, 5, 7 and 10 are matched with 300, etc. Is there some information missing? If so, could you provide a complete example, by which I mean an example which both can be copied, pasted, run and which adequately simulates your problem?
From: David Young on 28 May 2010 12:09 I can't think of a way to do it without looping, but I can certainly simplify your code, which might help with the timing. This does the task: >> ImportantNumb = [100 300 200]; >> NewId = [4 5 8 6 7 9 10]; >> for i = 1:length(NewId); ImportantNumb(NewId(i)) = ImportantNumb(i); end >> disp(ImportantNumb) There is a big assumption in there about the structure of NewId: each member of ImportantNumb needs to be set before it is accessed. If that's not true, you'll either get an error, or some zeros that perhaps you don't expect in the result.
From: Nick on 28 May 2010 12:14 Sorry about that...here's the code: %---------------------------- MatrixGuy=[ 1 100 2 300 3 200]; OldID=[1:7]'; NewID=[4 5 8 6 7 9 10]'; MatrixGuy=[MatrixGuy; NewID zeros(size(NewID,1),1)]; while sum(MatrixGuy(:,2)==0)>0 [~,b,c]=intersect(MatrixGuy(:,1),OldID); MatrixGuy(3+c,2)=MatrixGuy(b,2); end %------------------------------ It works that 4 is in the first row of NewID so you go to the first row of Old ID, see that it is 1 and then go to MatrixGuy and find the value in the 2nd column associated with ID1...it is 100 so you assign it to ID4. I have to loop because you'll see that ID6 is the newID for ID4 further down. I can't do it in one iteration because initially I don't know that ID4's value is 100, so I can't assign 100 to ID6 until I've already assigned 100 to ID4. And then on the next loop ID9 gets assigned 100 because it was formerly ID6 (which was formerly ID4....which was ID1...).
|
Pages: 1 Prev: The null() returns empty vector, how can I loose it Next: MATLAB PlC interface |