How can I change the path in a loop? MATLAB - matlab

i want to change the path of a file on each iteration in a loop. How can i do it?
for i=1:N
for j=1:M
image=imread('ORDENADOR/Sample001/img001-00001.png');
end
end
I want to change the Sample001 to Sample002,Sample003...until SampleN.Also i want to change img001-00001 until img001-M . Thank you very much

Consider making a cell array of images to read
imgnames = {'ORDENADOR/Sample001/img001-00001.png', ...
'ORDENADOR/Sample002/img001-00001.png' };
for i=1:length(imgnames)
image=imread(imgnames{i});
end
Alternatively, use num2str with format specifiers:
for i=1:N
for j=1:M
image=imread(['ORDENADOR/Sample' num2str(N,'%.3i') '/img001-' num2str(M,'%.5i') '.png');
end
end

for i=1:N
for j=1:M
image=imread(sprintf('ORDENADOR/Sample%03d/img%03d-%05d.png',i,i,j));
end
end

Related

How to compute the number of operations of jacobi iteration in matlab

I want to compute the number of operations of jacobi iteration in matlab
I do not know how to do it !!!
Can you help me ?
Thanks
Here is my code for newton method :
b=zeros(30,1);
b(6)=2;
alpha=1;
A=zeros(30,30);
A(1,1)=-(2+alpha);
A(1,2)=1;
for ii=2:29
A(ii,ii-1)=1
A(ii,ii)=-(2+alpha)
A(ii,ii+1)=1
end
A(30,29)=1;
A(30,30)=-(2+alpha);
D=diag(diag(A));
R=A-D;
x=zeros(30,1);
for ii=1:100
xk= inv(D)*(b-R*x);
if(norm(xk-x,1)<=10^-5)
break;
end
x=xk;
end
ii
You have counted it, actually.
In case of code
for ii=1:N
%% Code
end
the ii variable works like the counter.
Reading the code step-by-step you:
Define anonymous temporary array, say forII with elements 1 to N with default step of 1.
For each iteration the appropriate element of original forII is assigned to ii
%% Code is executed
New value of ii scalar is assigned.
This behaviour allows:
Use counter=1:N as true counter.
Loop easily over elements of any array, for example for foo='Hello World!'.
Use predefined indices, array elements, etc. for ii=A.
When the for loop is terminated by break or return prompt the elements in queue are not assigned and ii keeps the last assigned value.
You can try
kk=0;
for ii=1:N
kk=kk+1;
if ii==5
break
end
end
disp(['kk = ' num2str(kk) '; ii = ' num2str(ii)]);
kk=0;
for ii='Hello World!'
kk=kk+1;
if ii=='r'
break
end
end
disp(['kk = ' num2str(kk) '; ii = ' num2str(ii)]);

How to calculate iteration of gauss siedel method in matlab

I want to calculate iĀ“the number of iteration of gauss siedel method in matlab
here is my code
alpha=1;
A=zeros(30,30);
A(1,1)=-(2+alpha);
A(1,2)=1;
for i=2:29
A(i,i-1)=1;
A(i,i)=-(2+alpha);
A(i,i+1)=1;
end
A(30,29)=1;
A(30,30)=-(2+alpha);
D=diag(diag(A));
R=A-D;
x=zeros(30,1);
list=[];
count=0;
for k=1:150
xkk= inv(L)*(b-(U*x));
count=count+1;
list(count,:)=xk;
if(norm(xkk-x)<=10^-5)
break;
end
x=xkk;
end
count
when I ran it I got cound=150 so it seems that count does not works good
How to correct it?
Thanks
Change the last part like this:
xkk= inv(L)*(b-(U*x));
e=norm(xkk-x);
while(e>10^-5)
xkk= inv(L)*(b-(U*x));
count=count+1;
list(count,:)=xk;
e=norm(xkk-x)
x=xkk;
end

sorting nested cell as one arrange function

i have a mat data and extract 8 feature this file.
i should arrange this features as a cell and repeat that for 12 category
i want to arrange and uniform this code as one code?
feature_mean1=zeros(12,15);
for vmean1= 1:12
feature_mean1(vmean1,:)= mean(Catrgoryy1{vmean1});
end
feature_mean2=zeros(12,15);
for vmean2= 1:12
feature_mean2(vmean2,:)= mean(Catrgoryy2{vmean2});
end
%**********************
%***************
feature_min1=zeros(12,15);
for vmin1= 1:12
feature_min1(vmin1,:)= min(Catrgoryy1{vmin1});
end
feature_min2=zeros(12,15);
for vmin2= 1:12
feature_min2(vmin2,:)= min(Catrgoryy2{vmin2});
end
%***************
X=zeros(30,4);
h=1;
X_1=[feature_mean1(1,:)',feature_std1(1,:)',feature_min1(1,:)',feature_max1(1,:)',feature_mean2(1,:)',feature_std2(1,:)',feature_min2(1,:)',feature_max2(1,:)'];%
Y_1=repmat(1,length(X_1),1);
%%%**************222*************
X_2=[feature_mean1(2,:)',feature_std1(2,:)',feature_min1(2,:)',feature_max1(2,:)',feature_mean2(2,:)',feature_std2(2,:)',feature_min2(2,:)',feature_max2(2,:)'];
Y_2=repmat(2,length(X_2),1);
%%%**************333**************
.
.
.
X_12=[feature_mean1(12,:)',feature_std1(12,:)',feature_min1(12,:)',feature_max1(12,:)',feature_mean2(12,:)',feature_std2(12,:)',feature_min2(12,:)',feature_max2(12,:)'];
Y_12=repmat(12,length(X_12),1);
at first must be form 8 array for each of the Features
then insert all of them in the for loop
for o=1:12
Xf(o,:)=[feature_mean11{o},feature_std11{o},feature_min11{o},feature_max11{o},feature_mean22{o},feature_std22{o},feature_min22{o},feature_max22{o}];
end
finish

false position method in matlab

Can someone help me check where did I get wrong in the following code?
It is using False Position Method to find out the root of a function. Thanks
clc
clear all
format compact
x0=-2; x1=2; tol=10^-6; n=100;
f=#(x)x^3*5*x.^2+3*x-9;
y0=f(x0);
y1=f(x1);
i=1;
while i<=n
x=x1-y1*(x1-x0)/(y1-y0);
if abs(x-x1)<tol
disp('Root of function:');
disp(x);
break
end
i=i+1;
y=x^3*5*x.^2+3*x-9;
if y0*y1<0
x0=x;
y0=y;
else
x1=x;
y1=y;
end
end

How to use if statement when condition is a successful load of data? MatLab

So In a loop, I want all statements to be executed only if the load if data in that loop is successful. Else I want the loop to continue to the next iteration.
for l=1:.5:numfilesdata
if H(x,y)= load( ['C:\Users\Abid\Documents\MATLAB\Data\NumberedQwQoRuns\Run' num2str(t) '\Zdata' num2str(l) '.txt']);
%%%%%Converting Files
for x=1:50;
for y=1:50;
if H(x,y)<=Lim;
H(x,y)=0;
else
H(x,y)=1;
end
end
A(t,l)=(sum(sum(H))); %Area
R(t,l)=(4*A(t,l)/pi)^.5; %Radius
end
end
As you can see I am incrementing by .5, and if the load doesn't work on that increment I want the loop to essentially skip all the operations and go to the next step.
Thank You,
Abid
Check if the files exists before loading and processing:
if exist(filename,'file')
...
end
I'm not quite certain of this line:
if H(x,y)= load( [...]); %# This tries to load dat file at x,y position in `H`
x and y seem unknown at the first loop iteration, then fallback to 50,50 (last index of subsequent loop).
You may try:
H = load( [...]); %# This tries to load dat file in `H`
if numel(H) ~= 0
%# iterate over H
end
You could use a TRY/CATCH block:
for i=1:10
try
H = load(sprintf('file%d.txt',i), '-ascii');
%# process data here ...
catch ME
fprintf('%s: %s\n', ME.identifier, ME.message)
continue
end
end