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:
Multiplication

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 00:16:04

Message: 1 of 12

Hi there

I have two question one is simple& the other is somehow less simple :)
i have a simple question

i have two matrix each is 64x64 size

the first matrix N is a binary sequence matrix its max value is 1 & min -1
the second matrix N its max value is 0.0045 & min 0.002

i need to compute
N(i,j)M(i,j)

i tried this line code

N .* M ...... i got all zeros

is it simply computed as

N*M;

or i should use other command ?

i know it's simple question
but sometime when u dont get sure of the result u think u coded something wrong

The Second Question

i have matrix 64x64 and another one 256x256

i need to multiply them but as follow

every value in the smaller matrix corresponds to 4 values in the big matrix

it's like a window or something like that



thanx

Subject: Multiplication

From: dpb

Date: 14 Jun, 2011 00:47:29

Message: 2 of 12

Twilighter wrote:

> Hi there
>
> I have two question one is simple& the other is somehow less simple :)
> i have a simple question
>
> i have two matrix each is 64x64 size
>
> the first matrix N is a binary sequence matrix its max value is 1 & min -1
> the second matrix N its max value is 0.0045 & min 0.002
> i need to compute N(i,j)M(i,j)
>
> i tried this line code
>
> N .* M ...... i got all zeros
>
> is it simply computed as
> N*M;
> or i should use other command ?



.* multiplies on an element-by-element basis which I presume is what you
intend. More than likely the actual results you got aren't 0 but are
simply a figment of displaying insufficient significant digits.

If

y = x.*y; % where x and y are your matrices

try

y(1)

max(y)
min(y)

and see...

...


> i have matrix 64x64 and another one 256x256
>
> i need to multiply them but as follow
>
> every value in the smaller matrix corresponds to 4 values in the big matrix
>
> it's like a window or something like that
...


Yes, but you have to have a definition of how you want the window applied.

Try a very small example and post an input and desired output

--

Subject: Multiplication

From: reza

Date: 14 Jun, 2011 01:13:14

Message: 3 of 12

On Jun 13, 8:16 pm, "Twilighter " <mscmat...@gmail.com> wrote:
> Hi there
>
> I have two question one is simple& the other is somehow less simple :)
> i have a simple question
>
> i have two matrix  each is 64x64 size
>
> the first matrix N is a binary sequence matrix its max value is 1 & min -1
> the second matrix N its max value is 0.0045 & min 0.002
>
> i need to compute
> N(i,j)M(i,j)
>
> i tried this line code
>
> N .* M ...... i got all zeros
>
> is it simply computed as
>
> N*M;  
>
> or i should use other command ?
>
> i know it's simple question
> but sometime when u dont get sure of the result u think u coded something wrong
>
> The Second Question
>
> i have matrix 64x64 and another one 256x256
>
> i need to multiply them but as follow
>
> every value in the smaller matrix corresponds to 4 values in the big matrix
>
> it's like a window or something like that
>
> thanx

1. You are doing the correct operations for what you need. The reason
you are getting all zeros is probably because of type casting. For
example the following in matlab will produce a 0

>> int8(1) * 0.1

even though it looks like you are multiplying 1x0.1. To avoid this
type cast N into double first before the multiplication:

>> double(N).*M


2. You have said that every value in the smaller matrix maps to 4
values in the larger matrix. We dont know the mapping, nore do we know
if the a cell in the large matrix can have more than one thing mapped
to it. In addition you have not defined what kind of multiplication
you need; matrix or element-wise

In any case the bigger matrix has 16 times as many values as the
smaller one. Mapping 1 cell to 4 leaves a lot of cells unmapped in the
larger matrix.

/reza

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 01:25:21

Message: 4 of 12

hmmmm

i thought as i do multiply N(i,j)M(i,j)

then i need every pixel multiply it by the other & it's recorded at its position

like this
 N M Result
1 2 3 3 3 6
3 4 3 3 9 12

something like that



for the second example

i need to multiply every single value in the small matrix to 4 values in the big one


suppose this case

2x2 Matrix 8x8
2 3 2 3 4 5 5 6 7 8
5 7 4 5 6 7 5 6 7 1
                      2 5 8 4 6 9 7 1
                      1 4 7 8 5 9 6 2
                     .
                     .


so now

2 x 2 3 4 5 3x 5 6 7 8
        4 5 67 5 6 7 1
        2 5 8 4 6 9 7 1
        1 4 7 8 5 9 6 2
                                                      
5x ....... 7 x ........


so the number of pixel the one value from the smaller value will be multiply it in

equal NxM/KxL

in my Case 256x256/64x64 so one pixel in the small matrix will be multiply it in 4x4 window from the big matix

and the result is 256x256 matrix

is this example clear

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 01:47:05

Message: 5 of 12

thank you for yor reply Reza and dpb

sorry the page squeezed my example
here its

suppose this case

2x2
2 3
5 7

Matrix 8x8
2 3 4 5 5 6 7 8
4 5 6 7 5 6 7 1
2 5 8 4 6 9 7 1
1 4 7 8 5 9 6 2
                     .
                     .


so now

2 x
2 3 4 5
4 5 6 7
2 5 8 4
1 4 7 8
3x
5 6 7 8
5 6 7 1
6 9 7 1
5 9 6 2
                                                      
5x .......

7 x ........


so the number of pixel the one value from the smaller value will be multiply it in

equal NxM/KxL

in my Case 256x256/64x64 so one pixel in the small matrix will be multiply it in 4x4 window from the big matix

and the result is 256x256 matrix

is this example clear

Reza actually for the first question

i have an image img 64x64 type douple

i have other matrix M which has values from 0.002 & 0.0045

the image i convert it into binary by doing this

img = round(img./256)

then replace every 0 by -1 using this

img(img==0) = -1

so i get an image of values between -1 & 1 which is correctly displayed &looks the same as the original one

now i need to multiply the positive values in the matix M

when i display the image it's all place although all values are double

img, M the output image

i also tried to display it as imshow(uint8(img)) it still black

but its values are not zero it's between -0.0045 & 0.0045 ?

so why the image is black ?

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 01:57:04

Message: 6 of 12

sorry correcting my last paragraph cause i found it unreadable :D

so i get an image as a sequence of -1 & 1 which is correctly displayed &looks the same as the original one

now i need to multiply the image by the +ve values matix M which has min value = 0.002 & max= 0.0045

after multiplication when i display the image it's all black

but its values are not zero it's between -0.0045 & 0.0045 ?

M, img, out image all of type double so no casting problem here

i also tried to display it as imshow(uint8(img)) it still black

so why the image is black ?

Subject: Multiplication

From: ImageAnalyst

Date: 14 Jun, 2011 03:14:50

Message: 7 of 12

If you cast values in the rand [-0.5 to +0.5] to uint8, they will be
zero because uint8 can't take fractional numbers. Don't cast, just
display with autoscaling, like
imshow(img, []);

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 07:20:04

Message: 8 of 12

i tried to show it as double
as uint8

i mean i tried all options but it still black
so i said the problem isnot in the casting

it's in the way i multiply them?

Subject: Multiplication

From: ImageAnalyst

Date: 14 Jun, 2011 11:16:03

Message: 9 of 12

On Jun 14, 3:20 am, "Twilighter " <mscmat...@gmail.com> wrote:
> i tried to show it as double
> as uint8
>
> i mean i tried all options but it still  black
> so i said the problem isnot in the casting
>
> it's in the way i multiply them?
---------------------------------------------------------------
Are there non-zero values when you look in the Variable Editor or are
they all zero there?

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 11:28:04

Message: 10 of 12

Hi ImageAnalyst

no as i said the output matrix values were between 0.0045 & -0.0045

i found out where is the problem

it's in the values themselves it's considered to be near 0

cause when i tried to multiply it in a bigger number the image showed & it didnt look black anymore

thank you :)

and can u help me in my other question
about mapping

cause i had two questions that was the first one & there was another below it

Subject: Multiplication

From: ImageAnalyst

Date: 14 Jun, 2011 12:42:13

Message: 11 of 12

On Jun 14, 7:28 am, "Twilighter " <mscmat...@gmail.com> wrote:
> Hi ImageAnalyst
>
> no as i said the output matrix values were between 0.0045 & -0.0045
> i found out where  is the problem
> it's in the values themselves it's considered to be near 0
> cause when i tried to multiply it in a bigger number the image showed & it didnt look black anymore
------------------------------------------------------------------------------
Yes, but you wouldn't be getting the negative values if you simply
multiplied your image by (255/0.0045) - whatever value that is. ARE
YOU SURE you tried my suggestion of using bracket bracket like this
imshow(imageArray, []);
This is the way to do it and I'M REALLY SURPRISED if you say that it
didn't work. In fact it would be a bug if it didn't work and should
be reported.

Here's your second question:
"The Second Question
i have matrix 64x64 and another one 256x256. i need to multiply them
but as follow: every value in the smaller matrix corresponds to 4
values in the big matrix it's like a window or something like that "

I don't really know what this means. What do you mean by
"corresponds"? What do you mean by "multiply"? Do you really mean
"replicate" instead of "multiply"? Perhaps you're looking for the
imresize function with the 'nearest' option to upsample the small
image so that it's the same size at the original but looks blocky with
blocks of 4 pixels???

Subject: Multiplication

From: Twilighter

Date: 14 Jun, 2011 14:00:26

Message: 12 of 12

No ImageAnalyst i think you didnt read my message well ....

i have 2 matrix same size


matrix M has values from 0.002 & 0.0045

the other is an image i convert it into binary by doing this

img = round(img./256)

then replace every 0 by -1 using this

img(img==0) = -1

----------------------------------------------------------------

i did put an example to my second question

well the number of the pixel that will be multiplied by one value from the small matrix

is calculated as follow MxN / AxB

so in my case it will be 256x256 / 64x64 = 4x4

so each value in the small matrix will be multiply it by 4x4 in the big one

here is a small example

X = [2 3 ; 4 1]

Y = [ 2 3 4 5; 6 5 7 5; 1 1 1 1; 5 2 4 1 ]

4x4 / 2x2

so every pixel will be multiplied by 2x2 pixels

so
2 x [2 3; 6 5]
3 x [4 5; 7 5]
4 x [1 1; 5 2]
1 x [1 1; 4 1]

so the output

[4 6 12 15; 12 10 21 15; 4 4 1 1 ; 20 8 4 1 ]

how can i do this by code?

Tags for 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