Storing for loop calculations in a vector Matlab - matlab

I want to store each S I calculate in my for loop to store into a vector, so that I can plot S vs. x. i.e. S(x1),S(x1+0.1)..... will be stored as [S(x1) S(x1+0.1)....]
Here's my code:
function[S]=spline(m0,m1,p,q,x1,x2,x)
S=0;
x=x1;
for x=x1:.1:x2
S=(-m0/(6*(x2-x1)))*((x-x2)*(x-x2)*(x-x2))+(m1/(6*(x2-x1)))*((x-x1)*(x-x1)*(x-x1))+p*(x2-x)+q*(x-x1);
end
I can't seem to iteratively store each S in a vector. Thanks for any help.

You need to use an index for S. In you problem you can't use the one defined in your for loop so you need to use an other one. Also, if you want to plot it correctly you need to define one of your axis according to your 0.1 step.
function[S]=spline(m0,m1,p,q,x1,x2,x)
S=zeros(1,(x2-x1)/0.1 + 1));
x=x1;
i=1;
for x=x1:.1:x2
S(i)=(-m0/(6*(x2-x1)))*((x-x2)*(x-x2)*(x-x2))+(m1/(6*(x2-x1)))*((x-x1)*(x-x1)*(x-x1))+p*(x2-x)+q*(x-x1);
i=i+1;
end
end
You can then plot this vector : plot(linspace(x1,x2,(x2-x1)/0.1 + 1),S)

You are not indexing S in your loop. A cheap way to achieve what you want would be
idx = 0;
for x=x1:.1:x2
idx=idx+1;
S(idx)=...

Related

Finding local maxima for 3d matrix

I am trying to implement Hough transform in Matlab to find circles in a picture.
In the accumulator matrix, the global maximum is 105 at A(32,31,24). So I'm able to get this: max circle
The problem is, how can i find the local maxima to find the rest of the circles?
I wrote this to find A(i,j,k) which is bigger than the 26 adjacent points (26-Connected voxel neighborhood):
[i j k]=find(A~=0) ;
f=0;
for s=1:size(i)
if(i(s)~=100&&j(s)~=100&&k(s)~=141&&i(s)~=1&&j(s)~=1&&k(s)~=1)
if (A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)-1))
f=f+1
i(s)
j(s)
k(s)
end
end
end
Why i never got these correct i,j,k and f is always 0? I think i should at least find (32,31,24) and f=1?
Can anyone help me ?
Thank you so much!
The complete code is here:
im=imread('C:\Users\dell\Desktop\tp-complet\Hough\four.png');
sigma = 0.3;
gausFilter = fspecial('gaussian',[5 5],sigma);
sobelFilter=fspecial('sobel');
img=imfilter(im,gausFilter,'replicate');
ims=edge(img,'sobel');
rmax=size(im,1);
cmax=size(im,2);
radmax=round(sqrt(rmax^2+cmax^2));
for i=1:rmax
for j=1:cmax
for k=1:radmax
A(i,j,k)=0;
end
end
end
[r c]=find(ims==1);
length=size(r);
for k=1:length
for l=1:rmax
for m=1:cmax
if((l~=r(k))&&(m~=c(k)))
x=sqrt((l-r(k))^2+(m-c(k))^2);
x=round(x);
A(l,m,x)=A(l,m,x)+1;
end
end
end
end
[i j k]=find(A~=0) ;
f=0;
for s=1:size(i)
if(i(s)~=100&&j(s)~=100&&k(s)~=141&&i(s)~=1&&j(s)~=1&&k(s)~=1)
if (A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s))&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s))&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)+1)&&A(i(s),j(s),k(s))>A(i(s),j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s),k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s),j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)-1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)-1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)+1,k(s)-1)&&A(i(s),j(s),k(s))>A(i(s)+1,j(s)-1,k(s)-1))
f=f+1
i(s)
j(s)
k(s)
end
end
end
I don't have the image processing toolbox, so cannot readily run your code. However, a quick review indicates to me that you're using the find function incorrectly. When provided with 3 outputs (as you have it), find doesn't return the 3D indices, but rather row, column, and a vector of the values. That means that k is just a giant vector of 1's, and so your for loop's if k(s)~=1 statement is never satisfied. You should do something like [i,j,k]=ind2sub(size(A),find(A~=0)) if you instead want the 3D indices.
Also, FYI, this:
for i=1:rmax
for j=1:cmax
for k=1:radmax
A(i,j,k)=0;
end
end
end
Can be replaced with A=zeros(rmax,cmax,radmax);.

How to create a vector of the results in a for loop

I have a problem with the following code. I want to store all the values I am creating in the for loop below so that I can make a plot of it. I have tried several things, but nothing works. Does anyone know a simple method to create a vector of the results and then plot them?
dx=0.1;
t=1;
e=1;
for x=-1:dx:1
lower_bound=-100;
upper_bound=x/(sqrt(4*t*e));
e=1;
u=(1/sqrt(pi))*quad(#integ,lower_bound,upper_bound);
plot(x,u)
hold on
end
hold off
I would like to use as much of this matlab code as possible.
dx=0.1;
t=1;
e=1;
xval=[-1:dx:1].';
upper_bound = zeros(numel(xval),1);
u = zeros(numel(xval),1);
for ii=1:numel(xval)
x = xval(ii)
lower_bound=-100;
upper_bound(ii,1)=x/(sqrt(4*t*e));
u(ii,1)=(1/sqrt(pi))*quad(#integ,lower_bound,upper_bound(ii));
end
figure;
plot(xval,u)
by adding the (ii) behind your statements it saves your variables in an array. I did not use that on your lower_bound since it is a constant.
Note that I first created an array xval and called that with integers in ii, since subscriptindices must be positive integers in MATLAB. I also initialised both upper_bound and u by creating a zero matrix before the loop executes. This is handy since extending an existing vector is very memory and time consuming in MATLAB and since you know how big they will get (same number of elements as xval) you might as well use that.
I also got the plot call outside the loop, to prevent you from plotting 21 blue lines in 1 plot.

Permutation of pages of a 3d array in matlab

I have a 3D array representing an image in MATLAB. I want to reverse the position of pages(in my case slices).
Let's assume the number of pages is N. I want to replace first page with Nth, second with (N-1)th and so on.. Is there any function to do it in matlab. Now I am using the code below, but I have to avoid nested for loops, that is why I am looking for a prepared function. Any help would be appreciated.
Thank you in advance
I = ones(size(Image,1),size(Image,2),size(Image,3));
k=1;
for n=size(Image,3):-1:1
I(:,:,k) = Image(:,:,n);
k = k+1;
end
You can simply
I = Image(:,:,end:-1:1);
Another possibility, which lets you use the same notation for flipping the array along any dimension:
I = flipdim(Image, 3); %// 3 is the dimension you want to flip along

First Derivative filter Matlab

I have recently been tasked with using a first derivative filter on an image of myself. The instructor said that I should first fix the value of y and preform f(x+1) - f(x) on the rows and then fix the new "X" values and preform f(y+1)-f(y) on the columns.
Note: I have been asked to do this task manually, not using filter2() or any other programmed function, so please do not suggest that I use filter2() or similar. Thanks!
I tried calling up all the pixels and subtracting each successive one by doing
fid = fopen('image.raw')
myimage = fread(fid,[512 683], '*int8')
fclose(fid)
imsz = size(myimage)
x = imsz(1)
for I = 1:512
for J = 1:683
X(I) - X(I-1) = XX
But it doesnt seem to work, and I dont quite understand why. If you could help me, or point me in the right direction, I would be very appreciative.
First of all, your code is syntatically incorrect:
There is no end statement to any of your loops, and besides, you don't even need loops here.
You seem to read your image into the variable myimage, but you're using an undefined variable X when attempting to calculate the derivative.
The order of your assignment statements is reversed. The variable you wish to assign to should be written in the left hand part of the assignement.
I strongly suggest that you read online tutorials and get yourself familiar with MATLAB basics before taking on more complicated tasks.
As to your specific problem:
MATLAB encourages vectorized operations, i.e operations on entire arrays (vectors or matrices) at once. To subtract adjacent values in an array, what you're basically doing is subtracting two arrays, shifted by one element with respect to each other. For one dimensional arrays, that would translate in MATLAB to:
a(2:end) - a(1:end-1)
where a is your array. The end keyword specifies the last index in the array.
To compute the derivative of an image (a 2-D matrix), you need to decide along which axis you want to perform that operation. To approximate the derivate along the y-axis, do this:
X(2:end, :) - X(1:end-1, :)
You can verify that this gives you the same result as diff(X, 1) (or simply diff(X)). To compute the approximate derivative along the x-axis, which is equivalent to diff(X, 2), do this:
X(:, 2:end) - X(:, 1:end-1)
The colon (:) is the same as writing 1:end as the array subscript for the corresponding dimension.
If your filtered image is div then
for Y = 1:682
for X = 1:511
div(X, Y) = myimage(X + 1, Y + 1) - myimage(X,Y);
end
end
Remember the last row and the last column are not filtered!

How to implement this with a simple for loop

The problem is as follows:
(This is my first Matlab code)
So, I have a velocity fixes, e.g.
Mach=0.2
For that I have coefficients that I use further in calculations, e.g.
CL0=0.3956+0.0101*Mach+0.0657*Mach^2+0.1438*Mach^3;
CLa=5.7814+0.1402*Mach+0.959*Mach^2+2.008*Mach^3; etc.
these I use later when calculating roots of the characteristic polynomial of the matrix A.
Then I take the roots to calculate the following:
delta_SP=real(s(1));
omega_SP=imag(s(1));
omegan_SP=sqrt(delta_SP^2+omega_SP^2);
zeta_SP=-delta_SP/sqrt(delta_SP^2+omega_SP^2);
The problem is when I want to do this for several Mach numbers, ranging
Mach=0.2:0.1:0.8
can you please help me with a for loop?
Please try as below
i = 1;
CL0 = 0;
CLa = 0;
for Mach = 0.2:0.1:0.8
CL0(i) = 0.3956+0.0101*Mach+0.0657*Mach^2+0.1438*Mach^3;
CLa(i) = 5.7814+0.1402*Mach+0.959*Mach^2+2.008*Mach^3;
% and do all other calculations right over here according to your
% problem
i = i + 1;
end
Hope this helps . . .
So in your question you don't relate mach, CL0 and CLa to s and delta etc... but to generate all you CLa dna CL0 values you actually don't even need a loop:
Mach=0.2:0.1:0.8;
CL0=0.3956+0.0101*Mach+0.0657*Mach.^2+0.1438*Mach.^3;
CLa=5.7814+0.1402*Mach+0.959*Mach.^2+2.008*Mach.^3;
Notice I used .^ instead of just ^, the dot means perform the power operator for each element of the matrix rather than on the matrix as a whole.
I can't comment on those other lines of your code because I don't see how they connect?
Shouldn't use the word "Don't Know How" in the title, it strays people away from your post.