Difference between using (j,i) vs (i,j) in MATLAB - matlab

So I have this code, where my response Shrinkage is affected by 5 variables namely ORT,LT,PBT,RA and L. I am plotting a surface for response when 2 variables vary while others are constant. I am getting the wrong graph when i use (i,j) while defining the shrinkage. This problem is solved when i use (j,i) instead of (i,j). I wish to know why this is happening.
ORT=0:1.8:90;
LT=100:4:300;
PBT=54;
RA=0;
L=50;
i=1;
j=1;
Shrinkage=zeros(50,50);
for i=1:50
for j=1:50
Shrinkage***(i,j)***=-0.610-(0.000653*ORT(i))+(0.002238 *LT(j))-
(0.000255*RA)+(0.00497*L)+(0.01411*PBT)+(0.000003*ORT(i)*LT(j))-
(0.000044*LT(j)*PBT)-(0.000009*RA*RA)-(0.000001*LT(j)*LT(j))-
(0.000011*L*L)+(0.000010*ORT(i)*L)+(0.000007*RA*L)-(0.000078*PBT*L);
end
end
A=linspace(0,90,50);
B=linspace(100,300,50);
[ORT,LT]=meshgrid(A,B);
surfc(ORT,LT,Shrinkage);

This is issue with Matrix Indices vs. Axis Indices.
Pay attention when you use this or that (meshgrid vs. ndgrid).

Related

How can I make each array multiply by each other in a nested for loop, using MATLAB?

Currently, I'm working on a school project involving buck converters. As current increase through an inductor, its inductance decrease (most likely). Each phase is adding an inductor. By adding an inductor, I divide the current by each added inductor. The current is ramped from 0 to 500.
My issue with the following code is that it does not use each array value of i_L(i,j) correctly. I receive some negative values, which is absolutely wrong.
In example...
At 500 Amps with 10 phases, each inductor uses 50 amps. Now L will be designed after
i_L(i,j)=current(j)./phases(i)= 500/10=50amps
L(i,j)= (-9.22297516731983*10^(-16).*(50^(4)))+(9.96260934359008*10^(-14).*(50^(3)))-(3.6355216850551*10^(-12).*(50^(2)))+(9.0205832462444*10^(-12).*(50^(1)))+1.06054781561763E-07 = 1.04106*10^(-7)
and so on
creating 10x10 = 100 cells
clc; clear all;
phases=linspace(1,10,10);
current=linspace(0,500,10);
for j = 1:10
for i=1:10
i_L(i,j)=current(j)./phases(i);
L(i,j)=(-0.000000000000000922297516731983*(i_L(i,j).^(4)))+(0.000000000000099626093435900800*(i_L(i,j).^(3)))-(0.000000000003635521685055100000*(i_L(i,j).^(2)))+(0.000000000009020583246244400000*(i_L(i,j).^(1)))+0.000000106054781561763000000000;
end
end
Thank you!
Your matrix i_L(i,j) got values up to 500=500(current)/1(amp).
The polynomial you're using is generating negative solutions for values greater than 130.
So the operation is using each array value correct.
Maybe you should reevaluate the polynomial, if you're dissatisfied with the solution.
Try:
x=[0:1:500];
y=(-9.22297516731983*10^(-16).*(x.^(4)))+(9.96260934359008*10^(-14).*(x.^(3)))-(3.6355216850551*10^(-12).*(x.^(2)))+(9.0205832462444*10^(-12).*(x.^(1)))+1.06054781561763E-07;
plot(x,y)
You will see the polynomial will diverge against negative infinite for positive values.

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.

Matlab: Issues with filling a 401x401 matrix

I'm having an issue with filling a 401x401 matrix. I know exactly what I want to do but I'm struggling to implement it.
I would like for a specific angle (y axis from 30-70) and a specific wavelength (x axis from 400nm-1000nm) the matrix is filled to 401 x 401 to contain the associated reflection coefficient (I have the equations and they're all good).
I thought this would work#
for i=1:length(ANGLE)
angle=ANGLE(i);
etc etc
for i=1:length(wavelengths)
lambda=wavelengths(i);
etc etc
REF(i)=ref;
end
end
I hope you can help, sorry if this is badly worded.
Thanks
Carmel
As m_power pointed out, you should use a different iterator for the internal for loop (and also try to avoid using i and j, as they are commonly used to represent imaginary values). In addition to this, you should reference both a row and column entry for each reflection coefficient entry. Since you want angle in the vertical direction and wavelength in the horizontal direction, you could use something like this:
for ii=1:length(ANGLE)
angle=ANGLE(ii);
etc etc
for jj=1:length(wavelengths)
lambda=wavelengths(jj);
etc etc
REF(ii,jj)=ref;
end
end
Hope this helps.

Diffusion outer bounds

I'm attempting to run this simple diffusion case (I understand that it isn't ideal generally), and I'm doing fine with getting the inside of the solid, but need some help with the outer edges.
global M
size=100
M=zeros(size,size);
M(25,25)=50;
for diffusive_steps=1:500
oldM=M;
newM=zeros(size,size);
for i=2:size-1;
for j=2:size-1;
%we're considering the ij-th pixel
pixel_conc=oldM(i,j);
newM(i,j+1)=newM(i,j+1)+pixel_conc/4;
newM(i,j-1)=newM(i,j-1)+pixel_conc/4;
newM(i+1,j)=newM(i+1,j)+pixel_conc/4;
newM(i-1,j)=newM(i-1,j)+pixel_conc/4;
end
end
M=newM;
end
It's a pretty simple piece of code, and I know that. I'm not very good at using Octave yet (chemist by trade), so I'd appreciate any help!
If you have concerns about the border of your simulation you could pad your matrix with NaN values, and then remove the border after the simulation has completed. NaN stands for not a number and is often used to denote blank data. There are many MATLAB functions work in a useful way with these values.
e.g. finding the mean of an array which has blanks:
nanmean([0 nan 5 nan 10])
ans =
5
In your case, I would start by adding a border of NaNs to your M matrix. I'm using 'n' instead of 'size', since size is an important function in MATLAB, and using it as a variable can lead to confusing errors.
n=100;
blankM=zeros(n+2,n+2);
blankM([1,end],:) = nan;
blankM(:, [1,end]) = nan;
Now we can define 'M'. N.B that the first column and row will be NaNs so we need to add an offset (25+1):
M = blankM;
M(26,26)=50;
Run the simulation through,
m = size(blankM, 1);
n = size(blankM, 2);
for diffusive_steps=1:500
oldM = M;
newM = blankM;
for i=2:m-1;
for j=2:n-1;
pixel_conc=oldM(i,j);
newM(i,j+1)=newM(i,j+1)+pixel_conc/4;
newM(i,j-1)=newM(i,j-1)+pixel_conc/4;
newM(i+1,j)=newM(i+1,j)+pixel_conc/4;
newM(i-1,j)=newM(i-1,j)+pixel_conc/4;
end
end
M=newM;
end
and then extract the area of interest
finalResult = M(2:end-1, 2:end-1);
One simple change you might make is to add a boundary of ghost cells, or halo, around the domain of interest. Rather than mis-use the name size I've used a variable called sz. Replace:
M=zeros(sz,sz)
with
M=zeros(sz+2,sz+2)
and then compute your diffusion over the interior of this augmented matrix, ie over cells (2:sz+1,2:sz+1). When it comes to considering the results, discard or just ignore the halo.
Even simpler would be to simply take what you already have and ignore the cells in your existing matrix which are on the N,S,E,W edges.
This technique is widely used in problems such as, and similar to, yours and avoids the need to write code which deals with the computations on cells which don't have a full complement of neighbours. Setting the appropriate value for the contents of the halo cells is a problem-dependent matter, 0 isn't always the right value.