Tuesday, June 16, 2009

do till condition is met "continue" example

This is a nice example.



clear

y = 1:100;
count = 0
for j = 1:length(y)

if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end



This takes x until y(j)>50. After the condition is met it simply passes out.


Better explained by:
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end

continue keeps on taking values does not break the chain.
while break breaks it and comes out of the loop



Contrasted over the
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end


lastly;


clear
clc
y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)<20||y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end

this does not even work while continue will keep looking for the situation.
Break just breaks while continue keep working escaping to the next one and checking!

Thursday, June 11, 2009

saving .mat file as per the iteration

I wanted to save the mat files as the iteration progressed.
here is the quick solution:
it saves the mat file with number of iteration as the file name: viz: 100.mat, 200.mat etc.
This is handy when you want to compare the results of the intermediate results as you progress the iteration.





if rem(nn,SS/10)==0
disp(['iteration: ', num2str(nn), ' of ', num2str(SS)]);
save ([num2str(nn)])
end


Friday, March 20, 2009

How to show the highlighted content in the blogger post

I wanted to highlight certain texts in the blogger posts, mostly codes and others and was having difficulties.

I just looked into the source code of one of the blog and realized that this piece of code will work:


test for a table style highlights (contents here)



So, I can show the highlighted content in the blogger post

Monday, March 16, 2009

Use of figure handle in Matlab

I used to run the plots in matlab without having any figure handle, but this morning I realized that if I do not put the handle it wont display the picture correctly.
Somehow the figure handle is not coping with the compiz!


figure without handle



Disable compiz if such problems persist.

Friday, February 20, 2009

Playing with wav file in Matlab

To do:

READ THE AUDIO FILE
DISPLAY WAVEFORM
COMPUTE AND DISPLAY FREQUENCY DOMAIN AS FIGURE

% tHE VALIDITY YET TO BE TESTED...
clear all;


close all;


clc;


% import a audio

[y,Fs,nbit]=wavread('your_audio.wav');


% extract left channel only


left=y(:,1);


% calculate the time scale


Ts=1/Fs;


[nsamples,c]=size(y);


tscale=0:Ts:(nsamples-1)*Ts;


%plot left and right channel in time domain


plot(tscale,left,'r');


xlabel('Time (sec)');


ylabel('Amplitude');


%compute the FFT with the proper sampling frequency


fmax=Fs;


%how many points the FFT must be computed on?


N=nsamples;


tscale=0:Ts:(N-1)*Ts;


%N=number of FFT points:


% change it and test the different resolution


fscale=linspace(0,fmax/2,floor(N/2));


spectrum=fft(left,N);


%plot the spectrum of the signal using the appropriate


% frequency scale and module of the coefficients


%plot the magnitude


figure(2);


plot(fscale,abs((spectrum(1:length(fscale)))));


xlabel('Frequency Hz')


ylabel('Module')

Friday, November 28, 2008

Unique data and shuffle data problem in matlab



I had array of data with repeated values...
So, had to get non-repetitive arrays.
This could be done by :
b = unique(data);

for more one can read help unique. This is very useful function.

Moreover, I had to shuffle the data so that they are not perfectly aligned:
I found the function shuffle:

function x = shuffle(x)
%SHUFFLE Shuffle an array of values.
% x = shuffle(x) shuffles the vector x in random order.
% Since this function changes the vector x,
% it creates a copy of it.
n = length(x);
disp n
for index = 1:n
% a random number between i and n
r = index + floor((n-index+1) * rand());
% swap elements index and r
x([index r]) = x([r index]);
end

Surprisingly, it worked for whole matrix.
:)
Happy Friday!!!