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:
sum of matrix's elements

Subject: sum of matrix's elements

From: Konst

Date: 8 May, 2012 19:39:07

Message: 1 of 6

I have a matrix which has some elements the same so what I need to do is to calculate what these different values are so I use
a=unique(b)
Then I calculate how many times each value appears like this:
for i=1:length(a)
    N(i)=sum(b==a(i));
end
and then I want to calculate the sum of the number of events with value larger than the one in each element in a, and I did this:
for i=1:length(a)
    r(i)=sum(N(i:length(N)));
end
then I need to change my data like this
k=log(r)
and then plot (a,k)
Then I do some calculations and a linear fit on my curve but I don't get the result I want. I need a slope around 1 and it gives me 2.2
Any ideas why? Is any of the above commands wrong?
Thanks in advance!

Subject: sum of matrix's elements

From: Matt J

Date: 8 May, 2012 20:08:07

Message: 2 of 6

"Konst " <konstance1@hotmail.com> wrote in message <jobsor$cp0$1@newscl01ah.mathworks.com>...
> I have a matrix which has some elements the same so what I need to do is to calculate what these different values are so I use
> a=unique(b)
> Then I calculate how many times each value appears like this:
> for i=1:length(a)
> N(i)=sum(b==a(i));
> end
==============

It's not wrong, but you should do this with HISTC instead.



> and then I want to calculate the sum of the number of events with value larger than the one in each element in a, and I did this:
> for i=1:length(a)
> r(i)=sum(N(i:length(N)));
> end
============

You should do this with CUMSUM

Subject: sum of matrix's elements

From: Roger Stafford

Date: 8 May, 2012 20:28:07

Message: 3 of 6

"Konst " <konstance1@hotmail.com> wrote in message <jobsor$cp0$1@newscl01ah.mathworks.com>...
> .....
> Then I do some calculations and a linear fit on my curve but I don't get the result I want. I need a slope around 1 and it gives me 2.2
> Any ideas why? Is any of the above commands wrong?
- - - - - - - - - -
  As Matt indicates, there are certainly simpler ways of doing your computation. As to why your linear fit doesn't give you the slope you expect, that slope will depend entirely on the values in b. Double the values in b and you will halve the slope of k against a. What you need to correct are your expectations, not your computation results.

Roger Stafford

Subject: sum of matrix's elements

From: Konst

Date: 8 May, 2012 22:12:06

Message: 4 of 6

"Roger Stafford" wrote in message <jobvkn$q37$1@newscl01ah.mathworks.com>...
> "Konst " <konstance1@hotmail.com> wrote in message <jobsor$cp0$1@newscl01ah.mathworks.com>...
> > .....
> > Then I do some calculations and a linear fit on my curve but I don't get the result I want. I need a slope around 1 and it gives me 2.2
> > Any ideas why? Is any of the above commands wrong?
> - - - - - - - - - -
> As Matt indicates, there are certainly simpler ways of doing your computation. As to why your linear fit doesn't give you the slope you expect, that slope will depend entirely on the values in b. Double the values in b and you will halve the slope of k against a. What you need to correct are your expectations, not your computation results.
>
> Roger Stafford
Thank you both but the data in b are standard and not from a random generator. That is my professor who gave them to me has already run his own programm and found the slope around 1 which means that I'm not doing something right...Thanks though!

Subject: sum of matrix's elements

From: Roger Stafford

Date: 8 May, 2012 22:46:07

Message: 5 of 6

"Konst " <konstance1@hotmail.com> wrote in message <joc5nm$mqt$1@newscl01ah.mathworks.com>...
> Thank you both but the data in b are standard and not from a random generator. That is my professor who gave them to me has already run his own programm and found the slope around 1 which means that I'm not doing something right...Thanks though!
- - - - - - - - - - - -
  You should remember that matlab's logarithm function, 'log', gives logarithms to the base 'e', not 10. The factor log(10) = 2.3 which is suspiciously close to 2.2 is the adjustment you would have to make if you wanted log base 10. Or you could use matlab's 'log10' which does use base 10. Does that help?

Roger Stafford

Subject: sum of matrix's elements

From: Konst

Date: 9 May, 2012 17:39:07

Message: 6 of 6

"Roger Stafford" wrote in message <joc7ne$16d$1@newscl01ah.mathworks.com>...
> "Konst " <konstance1@hotmail.com> wrote in message <joc5nm$mqt$1@newscl01ah.mathworks.com>...
> > Thank you both but the data in b are standard and not from a random generator. That is my professor who gave them to me has already run his own programm and found the slope around 1 which means that I'm not doing something right...Thanks though!
> - - - - - - - - - - - -
> You should remember that matlab's logarithm function, 'log', gives logarithms to the base 'e', not 10. The factor log(10) = 2.3 which is suspiciously close to 2.2 is the adjustment you would have to make if you wanted log base 10. Or you could use matlab's 'log10' which does use base 10. Does that help?
>
> Roger Stafford

Yes, that was exactly my mistake! Thank you very much.

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