Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

doubts regarding matlab programming in image processing

Status
Not open for further replies.

M.Shobana

Member level 1
Joined
Sep 13, 2011
Messages
35
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,505
hello ,
I am facing problem when i am trying to find the number of occurence of 1 in binary matrix for particular row alone.Actually i used sum() function for counting the '1'.
this is my code
'' clear all
R= r(1,:) //for example i took 1st row in the 'r' matrix
G = sum(R);''

here its returns 'double' value instead of integer.if anybody knows please help me
 

Hello Shobana

- - - Updated - - -

Well I just take the ones matrix for the case, you can fit in your matrix here

Code:
 clc;
 r=ones(6);
 G=r(1,:);
 sum = 0;

 for i = 1: length(G)
     sum = sum + 1*G(i);
 end
 display ('Number of ones are :');
 display (sum);

Bests,
Shan
 

Hello Shobana

- - - Updated - - -

Well I just take the ones matrix for the case, you can fit in your matrix here

Code:
 clc;
 r=ones(6);
 G=r(1,:);
 sum = 0;

 for i = 1: length(G)
     sum = sum + 1*G(i);
 end
 display ('Number of ones are :');
 display (sum);

Bests,
Shan

Actually I tried in this method also but its returns only the value which is of double data type..the coding assumes 'G' as a character array and it convert the sum value in to double instead of integer
 

So why don't you want the double data type...any ways...

I was wondering the same thing. Why the curious fixation on the class (double in this case) of the object du jour? Who cares, since you can cast it to integer.

Does this troublesome double give you the correct number? If yes then rejoice because problem solved. Cast it to int and you're done. Also see: floor, ceil, round, fix.
 


Dunno, you tell me. All I know is that the Y you put there is not a 3D array as suggested in your previous question.

Best case the result will be Y itself (due to the index of 1 ==> first 2d "slice"). Worst case you get an error. Too lazy to start matlab right now to check, but you could of course do that. What with you actually being interested in the answer. ;-)

Update: Oh alright, I was rebooting anyways so might as well check... Lucky you, I was curious if it would throw an error ... or extend the 2D array to a 3D array.

Code:
>> Y =[1 3 5; 2 4 6; 10 1 1];
>> Y

Y =

     1     3     5
     2     4     6
    10     1     1

>> Y(:,:)

ans =

     1     3     5
     2     4     6
    10     1     1

>> Y(:,:,1)

ans =

     1     3     5
     2     4     6
    10     1     1

And just so you get the idea ...

Code:
>> Y(:,:,2)
??? Index exceeds matrix dimensions.

... obviously. Since as I suggested the Y you gave here is a 2 dimensional thingy, not a 3 dimensional thingy like you actually asked the question about.

PS: but tell me this, I am just trying to understand it.... Why the hell don't you plug this in matlab yourself? It really gets you the answer quicker than a forum... If you don't have matlab, install octave (which is a free matlab clone).
 
Last edited:

So why don't you want the double data type...any ways...
use this

Code:
x=int8(sum); % this will convert float to integer

Also see the link
https://www.mathworks.com/help/matlab/ref/int8.html;jsessionid=215359598097ac7ffb4842cec962
and got through similar functions

the above method is also not working.
'' R= r(1,:);
G = sum(R)''
for example r =[10111;10000;11111] then R = [10111] and for G i should get 4 as an answer but i am getting '392' this is my problem.i tried all conversion and normal addition using for loop also but it is not working....
 

r =[10111;10000;11111] then R = [10111]

Yes. But in what way do things not work? R = 10111, which is true, but I hope you realize that this is not magically a binary number just because you only have ones and zeroes in your very much DECIMAL NUMBER.

What you are doing is like saying 16'h1337 is a decimal number. While in fact it's a hex number, it's decimal notation being 4919. Just as your 10111 there is a decimal number, with a binary notation of 10011101111111. dec2bin(10111), now THAT is binary. :p Well actually it's not. It's of type char, but who's counting. ;)

Could you provide your entire matlab code? That'll save us some assumptions about your assumptions.

Update: Ah, sod it. Lets avoid a posting merry-go-round for something this trivial.

You want the sum of all the ones in your binary notation matrix, right?

1 - there's no binary notation in matlab
2 - there is however character arrays that you can use
3 - see below

Do NOT do this:

r =[10111;10000;11111]

But do as below. Note the quotes around your "binary" numbers. That way you get an array of type char. In general, to see what kind of type an object is you can use the class() function. Like so:

Code:
> r =['10111';'10000';'11111']
r =

10111
10000
11111

> class(r)
ans = char

You'll notice this is a 3x5 matrix of single characters, and there's the trick du jour. You want your sum? here we go:

Code:
> sum((r-'0')(:))
ans =  10

No need for silly loops and whatnot. You're welcome.

Updated update: Oh and since you may possibly also want this:

Given a decimal matrix Y, what is the total number of ones for "Y in binary notation"? Too easy.
Code:
Y =[1 3 5; 2 4 6; 10 1 1];
> dec2bin(Y)
ans =

0001
0010
1010
0011
0100
0001
0101
0110
0001

> sum((dec2bin(Y)-'0')(:))
ans =  13

There we go, 13 ones in there. That should do the trick...
 
Last edited:

Yes. But in what way do things not work? R = 10111, which is true, but I hope you realize that this is not magically a binary number just because you only have ones and zeroes in your very much DECIMAL NUMBER.

What you are doing is like saying 16'h1337 is a decimal number. While in fact it's a hex number, it's decimal notation being 4919. Just as your 10111 there is a decimal number, with a binary notation of 10011101111111. dec2bin(10111), now THAT is binary. :p Well actually it's not. It's of type char, but who's counting. ;)

Could you provide your entire matlab code? That'll save us some assumptions about your assumptions.

Update: Ah, sod it. Lets avoid a posting merry-go-round for something this trivial.

You want the sum of all the ones in your binary notation matrix, right?

1 - there's no binary notation in matlab
2 - there is however character arrays that you can use
3 - see below

Do NOT do this:

r =[10111;10000;11111]

But do as below. Note the quotes around your "binary" numbers. That way you get an array of type char. In general, to see what kind of type an object is you can use the class() function. Like so:

Code:
> r =['10111';'10000';'11111']
r =

10111
10000
11111

> class(r)
ans = char

You'll notice this is a 3x5 matrix of single characters, and there's the trick du jour. You want your sum? here we go:

Code:
> sum((r-'0')(:))
ans =  10

No need for silly loops and whatnot. You're welcome.

Updated update: Oh and since you may possibly also want this:

Given a decimal matrix Y, what is the total number of ones for "Y in binary notation"? Too easy.
Code:
Y =[1 3 5; 2 4 6; 10 1 1];
> dec2bin(Y)
ans =

0001
0010
1010
0011
0100
0001
0101
0110
0001

> sum((dec2bin(Y)-'0')(:))
ans =  13

There we go, 13 ones in there. That should do the trick...
please give me the coding for getting the number of ones in each row separately....not for whole matrix
 

Sure, glad to help!

Code:
sum((dec2bin(Y(1,:)-'0')(:))

That gives said sum for row 1 of Y.

Oh wait, you need row 2 as well right? Nono, no need to type it yourself.

Code:
sum((dec2bin(Y(2,:)-'0')(:))

There we go.

What's that, row 3 too? No worries, I am ON IT!

Code:
sum((dec2bin(Y(3,:)-'0')(:))

Anything other trivial matlab tidbits I can do for you?
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top