Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Need helps for some work in matrix

Subject: Need helps for some work in matrix

From: alfann

Date: 4 Jun, 2010 10:43:49

Message: 1 of 19

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?

Subject: Need helps for some work in matrix

From: Oleg Komarov

Date: 4 Jun, 2010 11:37:04

Message: 2 of 19

alfann <alfann.net@hotmail.com> wrote in message <1373922205.277491.1275648270975.JavaMail.root@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

Subject: Need helps for some work in matrix

From: alfann

Date: 4 Jun, 2010 12:10:56

Message: 3 of 19

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.

Subject: Need helps for some work in matrix

From: Oleg Komarov

Date: 4 Jun, 2010 13:38:05

Message: 4 of 19

> % 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

Subject: Need helps for some work in matrix

From: Oleg Komarov

Date: 4 Jun, 2010 13:40:23

Message: 5 of 19

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <huavjt$n6i$1@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

Subject: Need helps for some work in matrix

From: alfann

Date: 4 Jun, 2010 13:58:45

Message: 6 of 19

Thanksss Oleg Komarov,
but
the problem is:
The code that you built it, it works with one chain and it does not work with more than one chain.
How can you fix it to work with more than one chain.


See this matrix includes two chains:
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 two chains:
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]

Subject: Need helps for some work in matrix

From: Oleg Komarov

Date: 4 Jun, 2010 14:53:04

Message: 7 of 19

alfann <alfann.net@hotmail.com> wrote in message <1137948566.279085.1275659955694.JavaMail.root@gallium.mathforum.org>...
> Thanksss Oleg Komarov,
> but
> the problem is:
> The code that you built it, it works with one chain and it does not work with more than one chain.
> How can you fix it to work with more than one chain.

yes indeed, you can use the 'islands' function in order to find all the chains and then modify my solution in order to consider each chain separately.

islands: http://www.mathworks.com/matlabcentral/fileexchange/21376

% Isolate chains giving them a different index
[a,b] = islands(U1);
b(b(:,end) ~= 1,:) = [];
a(~ismember(a,b(:,1))) = 0;

Now you can work on each separate chain with my algorithm...you can use a loop (easier to adapt) or vectorize the code.

Oleg

Subject: Need helps for some work in matrix

From: alfann

Date: 4 Jun, 2010 15:50:23

Message: 8 of 19

I downloaded it but I do not know where I must put it in?
Which folder?

Could you please help me?

Subject: Need helps for some work in matrix

From: dpb

Date: 4 Jun, 2010 16:07:47

Message: 9 of 19

alfann wrote:
> I downloaded it but I do not know where I must put it in?
> Which folder?

Any one on the matlab path altho I would suggest creating one
specifically for such additional downloaded FEX contributions.
Certainly don't put it in a TMW-updated one...

--

Subject: Need helps for some work in matrix

From: alfann

Date: 4 Jun, 2010 16:47:29

Message: 10 of 19

Sorrrry for that,
but
I copied the file which I downliaded it and put it in my matlab path
then
I used the code that you gave me
but I do not know how can I use it.
Buceause I don't know how can I use the function and I read about it but still not understand.
sorry but could you please help me to solve it because I spent long time to solve it.

Subject: Need helps for some work in matrix

From: alfann

Date: 4 Jun, 2010 20:43:50

Message: 11 of 19

where are you????
waiting for you.
Please help me...

Subject: Need helps for some work in matrix

From: Walter Roberson

Date: 4 Jun, 2010 21:10:17

Message: 12 of 19

alfann wrote:
> where are you????
> waiting for you.
> Please help me...

Who is "you" in this matter? If you are referring to Oleg, he is in Italy,
where it is presently just a few minutes short of 11 pm on a Friday night.
Were you expecting Oleg to put aside his Friday evening plans in order to
assist you? If so, does he know that?


With regards to the islands program: did you read the example given in the
source code?

Subject: Need helps for some work in matrix

From: alfann

Date: 6 Jun, 2010 18:02:34

Message: 13 of 19

Dear members,
I downloaded the file, then I added its path to the matlab path.
Then, I copy this code
___________
[a,b] = islands(U1);
b(b(:,end) ~= 1,:) = [];
a(~ismember(a,b(:,1))) = 0;
___________

and run it and then print U1
and showd me U1 without any calculations.

Please...help me how can I do it
because my project needs to be ready this week by solve this problem.
Sorry if I bother you but because ny knowledg about the matlab is not like you here...
Your level is too good.
Please help me to run the code with this matrix:
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]



wiating for you.

Subject: Need helps for some work in matrix

From: alfann

Date: 7 Jun, 2010 10:55:37

Message: 14 of 19

Re: Need helps for some work in matrix
Posted: Jun 6, 2010 2:02 PM Plain Text Reply
 

Dear members,
I downloaded the file, then I added its path to the matlab path.
Then, I copy this code
___________
[a,b] = islands(U1);
b(b(:,end) ~= 1,:) = [];
a(~ismember(a,b(:,1))) = 0;
___________

and run it and then print U1
and showd me U1 without any calculations.

Please...help me how can I do it
because my project needs to be ready this week by solve this problem.
Sorry if I bother you but because ny knowledg about the matlab is not like you here...
Your level is too good.
Please help me to run the code with this matrix:
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]



wiating for you.
 
Please...

Subject: Need helps for some work in matrix

From: alfann

Date: 7 Jun, 2010 11:39:42

Message: 15 of 19

Hi
I will explain what I got and did:
I entered this matrix:
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]
______
then
I used this term:
[a,b] = islands(U1)

and it showed me this result:
a =

     0 0 0 0 0 0 0
     0 1 0 0 0 0 0
     0 1 0 0 2 2 0
     0 0 4 0 3 3 0
     0 4 4 0 5 5 5
     0 0 4 0 5 7 0
     0 0 0 6 6 7 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0

b =

     1 2 1
     2 2 21
     3 2 50
     4 4 50
     5 4 1
     6 2 50
     7 2 10

__________
then:
I printed:
b(b(:,end) ~= 1,:) = []

and the reults:
b =

     1 2 1
     5 4 1
which means the chains of ones are two and it is true.
________________

Then I printed:
a(~ismember(a,b(:,1))) = 0
which set all other numbers out of the chain equal to 0 zero.
a =

     0 0 0 0 0 0 0
     0 1 0 0 0 0 0
     0 1 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 5 5 5
     0 0 0 0 5 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0







My question is:
How can tell the program set all the two chains ones (1) no (1 and 5)?

and


How can I calculate the average for each chain?


Please help me....

Subject: Need helps for some work in matrix

From: Steven Lord

Date: 7 Jun, 2010 13:23:15

Message: 16 of 19


"alfann" <alfann.net@hotmail.com> wrote in message
news:248882707.291355.1275910812615.JavaMail.root@gallium.mathforum.org...
> Hi
> I will explain what I got and did:

*snip*

> Then I printed:
> a(~ismember(a,b(:,1))) = 0
> which set all other numbers out of the chain equal to 0 zero.
> a =
>
> 0 0 0 0 0 0 0
> 0 1 0 0 0 0 0
> 0 1 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 0 0 0 5 5 5
> 0 0 0 0 5 0 0
> 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0
>
> My question is:
> How can tell the program set all the two chains ones (1) no (1 and 5)?

So you want to change the 5's to 1's?

a(a==5) = 1;

Or do you want to change all non-zeros to 1's?

a = sign(a);
% or
a(a ~= 0) = 1;

> and
>
>
> How can I calculate the average for each chain?

The average _what_ for each chain?

It sounds vaguely like you're doing some sort of image processing problem,
in which case you might want to look at REGIONPROPS:

http://www.mathworks.com/access/helpdesk/help/toolbox/images/regionprops.html

or some of the functions in the Morphological Operations or ROI-Based,
Neighborhood, and Block Processing lists of functions on that documentation
page.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Need helps for some work in matrix

From: alfann

Date: 8 Jun, 2010 12:05:10

Message: 17 of 19

could u explain this line from the code:
neighB = U1(unique(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2))))


I want know this part:
neighB(~IDX,1)

what does it mean?


and


neighB(~IDX,2)

Subject: Need helps for some work in matrix

From: Walter Roberson

Date: 8 Jun, 2010 12:36:07

Message: 18 of 19

alfann wrote:
> could u explain this line from the code:
> neighB = U1(unique(sub2ind(size(U1),neighB(~IDX,1),neighB(~IDX,2))))
>
>
> I want know this part:
> neighB(~IDX,1)
>
> what does it mean?
>
>
> and
>
>
> neighB(~IDX,2)

~IDX is the same as IDX==0. Beyond that, please look up "logical indexing".

Subject: Need helps for some work in matrix

From: alfann

Date: 13 Jun, 2010 16:18:15

Message: 19 of 19

Hi there,
I want to show the code which created it from your helps, and I need onething to complete it please.

In this code:
________________________
%To give each chain special number in U3 matrix and save it in ax matrix
                    [ax,bx] = islands(U3);
                    bx(bx(:,end) ~= 1,:) = [];
                    ax(~ismember(ax,bx(:,1))) = 0;
                    
                    %To calculate the neighbours, then the average of each chain
                    for val=1:max(ax(:))
                        % rows and cols for each 1
                        [r, c] = find(ax == val);
                        % 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 U2
                        U2(ax == val) = average;
____________________________

In this code, I want to know if the chain started from the top of the matrix (ax) or from the bottom or between the top and the bottom of (ax) matrix.

*If any chain started from the top of (ax) matrix, the substitution in (U2) matrix will be 100 for the whole chain positions.

*If any chain started from the bottom of (ax) matrix, the substitution in (U2) matrix will be 0 for the whole chain positions.

*If any chain started between the top and the bottom of (ax) matrix, the substitution in (U2) matrix will be as it described in the above code.

How can I do that?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us