updating a second matrix in specific lines as you loop through a first one
Show older comments
I have a matrix lets call it AAA (n by 3 matrix) it looks a bit like the first one below called AAA - the important bit is coloumn 1 which contains numbers between say 1 and 20 such numbers can repeat.
AAA =
1 100 102
4 55 58
11 339 341
3 55 56
2 50 53
11 123 127
14 55 59
I have a second matrix lets call it BBB (20 by 2 coloumn 2 is all zero's for the moment)where I have populated only the first coloumn with the numbers 1 through 20.
BBB =
1 0
2 0
3 0
4 0
...
20 0
I want to loop through matrix AAA and populate coloumn 2 in matrix BBB with the number in coloumn 2 from matrix AAA next to its appropirate number. At each update I will sum up coloumn 2 in matrix BBB (which i can obviously handle). So in the above example after the first loop matrix BBB will have the number 100 in row 1 coloumn 2. After the second loop it will have the number 55 in coloumn 2 of row 4 after the next loop it will have 339 in coloumn 2 of row 11. As mentioned I will do something after each loop or update but that bit I can handle.
So it will start with something like this
[rows cols] = size(AAA) for i = 1:rows "then not sure if i should be using something like a string compare or a map.objects or what the most appropriate way to continue is"
Accepted Answer
More Answers (1)
Teja Muppirala
on 22 Apr 2013
There is also the very useful ACCUMARRAY command:
AAA = [ 1 100 102
4 55 58
11 339 341
3 55 56
2 50 53
11 123 127
14 55 59];
N = 20; % Or whatever size you want BBB
BBB = [(1:N)' accumarray(AAA(:,1),AAA(:,2),[N 1])]
1 Comment
matthew arnott
on 25 Apr 2013
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!