From: alfann on 4 Jun 2010 02:43 If I have this matrix: ______________________ U1=[20 33 50 20 10 80 10; 10 1 1 1 90 10 90; 60 1 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] and Now: I want to calculate the average of the summation of the neighbors for ones values( when the value=1). example1 (one chain): In the above matrix, we see the ones are in : U1(3,2)=1 and U1(2,2)=1 and U1(2,3)=1 and U1(2,4)=1 now, I want calculate the summation of the neighbors of ones, and it will be: average=(5+60+10+33+50+20+90+33+20+5)/10; Then U1 becomes: U1=[20 33 50 20 10 80 10; 10 32.6 32.6 32.6 90 10 90; 60 32.6 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] ______________________ ______________________ Example 2 (one chain): U1=[20 33 50 20 10 80 10; 10 1 85 88 90 10 90; 60 1 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] In this matrix, we see the ones are in : U1(3,2)=1 and U1(2,2)=1 only now, I want calculate the summation of the neighbors of ones, and it will be: average=(5+60+10+33+85+20)/6; Then change the one values to the (average), which means: U1 becomes: U1=[20 33 50 20 10 80 10; 10 35.5 85 88 90 10 90; 60 35.5 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] ______________________ ______________________ Example 3 (two chains) more clear: U1=[20 33 50 20 10 80 10; 10 1 85 88 90 10 90; 60 1 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 1 1 1; 20 0 50 60 1 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] In this matrix, we see the ones are in : First chain: U1(3,2)=1 and U1(2,2)=1 second chain: U1(6,5)=1 and U1(5,5)=1 and U1(5,6)=1 and U1(5,7)=1 now, I want calculate the summation of the neighbors of ones, and it will be: For first chain: average=(5+60+10+33+85+20)/6; and For second chain: average=(50+60+10+50+50+10+50+10)/8 Then the U1 matrix becomes: U1=[20 33 50 20 10 80 10; 10 35.5 85 88 90 10 90; 60 35.5 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 36.25 36.25 36.25; 20 0 50 60 36.25 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] How can I do that?
From: Oleg Komarov on 4 Jun 2010 07:37 alfann <alfann.net(a)hotmail.com> wrote in message <1373922205.277491.1275648270975.JavaMail.root(a)gallium.mathforum.org>... > If I have this matrix: > ______________________ > U1=[20 33 50 20 10 80 10; 10 1 1 1 90 10 90; 60 1 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] > and Now: > I want to calculate the average of the summation of the neighbors for ones values( when the value=1). > > example1 (one chain): > In the above matrix, we see the ones are in : > U1(3,2)=1 > and > U1(2,2)=1 > and > U1(2,3)=1 > and > U1(2,4)=1 > > now, I want calculate the summation of the neighbors of ones, and it will be: > average=(5+60+10+33+50+20+90+33+20+5)/10; > Then > U1 becomes: > U1=[20 33 50 20 10 80 10; 10 32.6 32.6 32.6 90 10 90; 60 32.6 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] Why do you count the 5 twice? BTW here's a solution (if I'm correct), up to you to shorten the code: % rows and cols for each 1 [r, c] = find(U1 == 1); % four connected neighbours neighB = [r-1 c; r+1 c; r c-1; r c+1]; % Exclude those out of matrix boundaries IDX = any(neighB < 1,2) | neighB(:,1) > size(U1,1) | neighB(:,2) > size(U1,2); % Take all the neigbours neighB = U1(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2))); % Average excluding the ones IDXones = neighB == 1; average = mean(neighB(~IDXones)); % Substitute into U1 U1(IDXones) = average; Oleg
From: alfann on 4 Jun 2010 04:10 Thanks for that, but the result is not right. apply it for this example: U1=[20 33 50 20 10 80 10; 10 1 1 1 90 10 90; 60 1 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] then apply the code which you wrote it: % rows and cols for each 1 [r, c] = find(U1 == 1); % four connected neighbours neighB = [r-1 c; r+1 c; r c-1; r c+1]; % Exclude those out of matrix boundaries IDX = any(neighB < 1,2) | neighB(:,1) > size(U1,1) | neighB(:,2) > size(U1,2); % Take all the neigbours neighB = U1(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2))); % Average excluding the ones IDXones = neighB == 1; average = mean(neighB(~IDXones)); % Substitute into U1 U1(IDXones) = average; then the result is not correct. Note: If the neigbour is equal to 1, we must neglect it.
From: Oleg Komarov on 4 Jun 2010 09:38 > % Take all the neigbours > neighB = U1(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2))); > then > the result is not correct. Substitute the row with: neighB = U1(unique(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2)))); Oleg
From: Oleg Komarov on 4 Jun 2010 09:40 "Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <huavjt$n6i$1(a)fred.mathworks.com>... > > % Take all the neigbours > > neighB = U1(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2))); > > > then > > the result is not correct. > > Substitute the row with: > neighB = U1(unique(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2)))); > > Oleg There's another error, I'll repost the whole code: U1=[20 33 50 20 10 80 10; 10 1 1 1 90 10 90; 60 1 20 33 21 21 30; 50 05 50 44 50 50 10; 80 50 50 10 50 90 10; 20 0 50 60 30 10 50; 10 50 30 50 50 10 90; 80 20 10 70 80 50 30; 40 50 30 80 20 10 20] % rows and cols for each 1 [r, c] = find(U1 == 1); % four connected neighbours neighB = [r-1 c; r+1 c; r c-1; r c+1]; % Exclude those out of matrix boundaries IDX = any(neighB < 1,2) | neighB(:,1) > size(U1,1) | neighB(:,2) > size(U1,2); % Take all the neigbours neighB = U1(unique(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2)))); % Average excluding the ones IDXones = neighB == 1; average = mean(neighB(~IDXones)); % Substitute into U1 U1(U1 == 1) = average; Oleg
|
Next
|
Last
Pages: 1 2 3 4 Prev: Identify colors in an RGB image Next: how to randomize random binary bits |