|
"Adham " <adham.atyabi@flinders.edu.au> wrote in message <ir7e2i$t12$1@newscl01ah.mathworks.com>...
> "Adham " <adham.atyabi@flinders.edu.au> wrote in message <ir7cv0$qcl$1@newscl01ah.mathworks.com>...
> > Hi
> >
> > I have a matrix called Mask and I need to change the current range of data inside this matrix to the range of [1,500] (no instant of matrix should be less than 1 and non should be greater 500). I know that it is possible to use mapminmax but it will convert the range to -1 and 1. Is there any direct way to do this?
> > I also tried mapminmax(Mask,1,500) but it did not do the job
> >
> > Thanx
> > Adham
>
> I found this equation in one of the answers, can someone please let me know if I am implementing it in a right way?
>
> y=(ymax-ymin)*(x-ymin)/(xmax-xmin)+1
>
> % Mask is 10*30 and I am going trough each row one by one and rescaling it
> for j=1:10
> x=Mask(j,:);
> for k=1:size(x,2)
> x(1,k) = (size(d1.data,2)-1)*(x(1,k)-1)/(max(x)-min(x)) + 1;
> end
> % it looks like the values are in the right range if you ignore their sign
> Mask2(j,:)=abs(x(:))';
> end
>
> Cheers
> Adham
- - - - - - - - - - -
According to the thread at:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/304452
a bug has been reported in 'mapminmax' for version matlabr2010b, which is probably why yours didn't work.
Also it looks as though you copied the document formula incorrectly. It should read:
y = (ymax-ymin)*(x-xmin)/(xmax-xmin) + ymin;
with ymax and ymin being your 500 and 1, respectively, and where xmax and xmin are column vectors of the maximum and minimum values along each of the rows of x.
You can implement the above formula without using for-loops if you make use of matlab's 'bsxfun' function:
t = min(x,[],2);
y = (ymax-ymin)*bsxfun(@rdivide,bsxfun(@minus,x,t),max(x,[],2)-t)+ymin;
This only works if none of the rows of x have constant values - otherwise you will get some NaNs.
You can test the results by checking that each row of y will attain both the values ymax and ymin somewhere within the row and that nowhere do y values fall outside that range.
Roger Stafford
|