Normalize values in a matrix to be between 0 and 1 (Matlab Tips)

I’m run a function in Matlab and it requires a matrix of variables within 0 and 1. The way to normalize the values to be between 0 and 1, and transform back to the original matrix as follows:

b = [10; 0; 1; 1];

%% normalized matrix B to 0 and 1
normB = b - min(b(:));
normB = normB ./ max(normB(:))

%% transform normalized matrix B to the original matrix
B_sim = normB_sim .* max(b(:))

Make sure, when transforming normalized matrix to the original matrix, you provided the maximum value of the original matrix. It not, the code cannot be run.

Original source: (Link) 

Please follow and like us:

How to export string with timeperiod to .dat file? (Re-Post)

Here a question (original link) that I ask at Matlab Community that the reader may found useful.


Question:

I have a large string that containing time period such as 23-May-2009 02:00:00 (as attached). Variable editor in Matlab is unable to display these variables, since the variables have more that 524288 elements. I want to export the variables to .dat file, so I can see the list of the variables. I try to use fopen and fprintf functions to export the variables, but unsuccessful. Any help? Thank you.

Answer:

load hourlyperiod.mat
d = hourlyperiod ;
fid = fopen('iwant.dat','w') ;
for i = 1:length(d)
 fprintf(fid,'%s \n',d(i,:)) ;
end
fclose(fid) ;
Please follow and like us:

Grouping boxplot in matlab

Here an example from http://stackoverflow.com/questions/16267440/grouping-boxplot-matlab. The example is about on how to create a boxplot for multiple group of variables. The example as follows:

x=rand(1000,6); %variable x
y=rand(1000,6); %variable y
D(:,1:2:12)=x; %rearrange x into d.
D(:,2:2:12)=y; %rearrange y into d

% first column of D, will fill with column of x, then second... 
% column of D will fill with 1st column of y. then the sequent... 
% will follow.

and

figure('color',[1,1,1]); %create a blank graph
boxplot(D,'factorgap',10,'color','rk') %make a box plot on those blank graph
axis([0 25 -1 5]) %not compulsory:rearrange back axis 

set(gca,'xtick',1.8:4.3:50) %put xtick to x-axis
set(gca,'ytick',0:10) %put tick to y-axis
set(gca,'xticklabel',{'Direct care','Housekeeping','Mealtimes','Medication','Miscellaneous','Personal care'}) %label to each x-axis

ylabel('Normalised Y');
 legend(findobj(gca,'Tag','Box'),'HBN04-01 multibed','YAB single ') %put legend to figure
Please follow and like us:

Run for 100 times (loops) and save each result of run into a single matrix in MATLAB

Here a simple example: You want to run 100x times of this function, y=2i, where i is integer values (0 until 100) and you want to save each y into a single matrix (named as y1), you can write as follow:

for i=1:100 % 100 times of loops 
y=2*i % our function
y1(i,:)=y % each y will save on a single column in the matrix end
end
Please follow and like us:

Read/edit the built-in Matlab functions

I found a good tip from the matlab community to find out the built-in function in Matlab. You can edit and change it. The tip as follows:

% You can use the [which] command to find the path name to the

% built-in function in question, i.e. -

which fft

% then point Matlab to that folder on your computer so that

% the fft.m file is in your "current folder" window. Then use

% the [edit] command to edit the function, i.e. -

edit fft

% The code for the fft function will appear in the editor window.

% Often it is useful to make small changes to the built-in code

% then comment out the changes as soon as you're done so that

% Matlab will continue to function as designed.
Please follow and like us: