The Student Room Group

Matlab dividing matrices

Hello,
I have a little problem with my work. I have 2 matrices G1 and G2, I have to sum these matrices (G=G1+G2). Let's take a look at 1st cell, I have 2 numbers 1023 and 1026, so the sum of 1st cell is 2049. Now i have to divide it ... I use G=G/.2, which kind of works BUT in the final G matrix in 1st cell is 10245 instead of 1024.5, is there a way to fix? Because I have no idea where is the problem.
Thank you for helping me :-)

EDIT: Sorry for not posting it in "Webmaster, Coding and Software Dev " did not see that category before :frown:
(edited 9 years ago)

Scroll to see replies

Reply 1
Original post by matzas
Hello,
I have a little problem with my work. I have 2 matrices G1 and G2, I have to sum these matrices (G=G1+G2). Let's take a look at 1st cell, I have 2 numbers 1023 and 1026, so the sum of 1st cell is 2049. Now i have to divide it ... I use G=G/.2, which kind of works BUT in the final G matrix in 1st cell is 10245 instead of 1024.5, is there a way to fix? Because I have no idea where is the problem.
Thank you for helping me :-)

EDIT: Sorry for not posting it in "Webmaster, Coding and Software Dev " did not see that category before :frown:


You probably need to select each element from the matrix i.e. G= G1(n) + G2(n) (this is assuming it is a vector with just one row for example)

however since it is a matrix i.e. more than 1 row and 1 column you will need a loop to go round selecting elements n times and then performing the addition with each corresponding element position.
(edited 9 years ago)
Reply 2
It is kinds of huge matrix, it is sensor data from a camera, around 1300x1600. So you suggest to do it G(n,i)=G1(n,i)+G2(n,i)? or something like that?

Ye will try it right away, thought I could simply use G=G1+G2, since in theory it should do the same
(edited 9 years ago)
Reply 3
Original post by matzas
It is kinds of huge matrix, it is sensor data from a camera, around 1300x1600. So you suggest to do it G(n,i)=G1(n,i)+G2(n,i)? or something like that?


Yeah I would try that


Original post by matzas

Ye will try it right away, thought I could simply use G=G1+G2, since in theory it should do the same

The thing with matlab is what's so obvious to us humans is not so obvious to the program lol
Reply 4
Ok, it worked, kind of stupid that i need 6 lines instead of 1 for only showing decimal
Reply 5
Original post by matzas
Ok, it worked, kind of stupid that i need 6 lines instead of 1 for only showing decimal


Nice one, did you use a nested for loop? Mine's stuck and says the index is out of bounds :lol:
Reply 6
s=size(G1);
for i=1:s(2)
for n=1:s(1)
G(n,i)=(G1(n,i)+G2(n,i))/2;
end
end

I wrote it that way.
Reply 7
Original post by matzas
s=size(G1);
for i=1:s(2)
for n=1:s(1)
G(n,i)=(G1(n,i)+G2(n,i))/2;
end
end

I wrote it that way.


yeah that's how i wrote mine lol...

when you say s(2) do you mean size(G1) *2 or are you referring to size(G2)?
Reply 8
Both matrices are same so I was referring to 2nd value in vector s=size(G1)

But to put this aside, I was looking why G=(G1+G2)/.2; did not work and I found out why. Well not sure why exacly not, because I was pretty sure I was supposed to write it this way, but I tried G=(G1+G2)/2; just to try everything I was able to think about and it worked, so i do not need to use these for cycles :-)
Reply 9
Original post by matzas
Both matrices are same so I was referring to 2nd value in vector s=size(G1)

But to put this aside, I was looking why G=(G1+G2)/.2; did not work and I found out why. Well not sure why exacly not, because I was pretty sure I was supposed to write it this way, but I tried G=(G1+G2)/2; just to try everything I was able to think about and it worked, so i do not need to use these for cycles :-)


Yes , it's because you only need to use the "." when performing element operations outside of loops. Once in a loop you don't need the dot as it comes up with an error if you do use it.
Reply 10
well, problem is that it is in no loop at all
skala_cervena = str2double(skala_cervena);
skala_zelena1 = str2double(skala_zelena1);
skala_modra = str2double(skala_modra);
skala_zelena2 = str2double(skala_zelena2);
s = system('dcraw -4 -D -T kob_85_L_14_12_2009_01.CR2');
raw = double(imread('kob_85_L_14_12_2009_01.tiff'));
R=raw(1:2:end, 1:2:end);
B=raw(2:2:end, 2:2:end);
G1=raw(1:2:end, 2:2:end);
G2=raw(2:2:end, 1:2:end);
G=(G1+G2)/2;


few lines of my code and with the "." there was no error as well, only every cell was multipled by 10 (like if i did (G1+G2)/2*10)
Reply 11
Original post by matzas
well, problem is that it is in no loop at all


few lines of my code and with the "." there was no error as well, only every cell was multipled by 10 (like if i did (G1+G2)/2*10)


hmm that's odd although I've never seen the bottom function before ("raw")!

I assigned a 3x3 matrix to a variable and when i type in the size function matlabs says my matrix is 3 by 1 when it's actually 3x3? :facepalm2:

I have no idea what it's doing today :lol:
Reply 12
well if you mean the line "s = system('dcraw -4 -D -T kob_85_L_14_12_2009_01.CR2');" it basicly opens CMD and executes programm DCRAW with parameters -4 -D -T kob_85_L_14_12_2009_01.CR2. I have to use external programm with matlab because matlab cannot read RAW data, I have to transform to somemthing he can read, in this case to TIFF.
Reply 13
Original post by matzas
well if you mean the line "s = system('dcraw -4 -D -T kob_85_L_14_12_2009_01.CR2');" it basicly opens CMD and executes programm DCRAW with parameters -4 -D -T kob_85_L_14_12_2009_01.CR2. I have to use external programm with matlab because matlab cannot read RAW data, I have to transform to somemthing he can read, in this case to TIFF.


Ahh I see, are you studying engineering btw? :tongue:
Reply 14
Biomedical Technology and Bioinformatics :-) this work is about Digital photo processing of retina.
Reply 15
Original post by matzas
Biomedical Technology and Bioinformatics :-) this work is about Digital photo processing of retina.


oohhhh sweet, I learned a little bit about this in my maths lecture and how the eye picks up different intensity light levels :biggrin:
I hate MATLAB...
Reply 17
Ye This topic is pretty interesting, the main goal for this soft is that doctors/whoever works with pictures on retina can adjust manually the picture without compromising it with unecessary white balancing, etc, basicly the whole process of transfering RAW sensor data to JPEG/TIFF or whatever you want. The soft will get RAW data, in my case Canon RAW data in *.CR2 and my goal is to make GUI in the end with options for user to set values manually or some kind of autotransfer to TIFF
Reply 18
Original post by Dima-Blackburn
I hate MATLAB...


Do not get me wrong, but matlab is awesome for researchers and lab work
Reply 19
Original post by Dima-Blackburn
I hate MATLAB...


it can be annoying sometimes... :lol:


Original post by matzas
Ye This topic is pretty interesting, the main goal for this soft is that doctors/whoever works with pictures on retina can adjust manually the picture without compromising it with unecessary white balancing, etc, basicly the whole process of transfering RAW sensor data to JPEG/TIFF or whatever you want. The soft will get RAW data, in my case Canon RAW data in *.CR2 and my goal is to make GUI in the end with options for user to set values manually or some kind of autotransfer to TIFF


guessing youre a postgraduate? O.o

Quick Reply

Latest

Trending

Trending