expand vector with interpolated values - matlab

I have two vectors that happen to be lat and long that I extracted from a shape file. I would like to interpolate the values so I have the same resolution as the points from which I am finding the distance.
I found this question and another question that almost solve my problem. The difference is I do not know the spacing between my vector points (they vary) and I want to make sure I always have at most 0.0042 deg between points. Below are the first 10 points of my lat/long vectors.
latlim=interp1(latlim,1:0.001:numel(latlim)); gets me what I want to avoid any aliasing but I'd like to get exactly 0.0042 between points rather than just 1000 new points for the sake of efficiency. I have a lot of points to iterate on.
latlim=[78.1530 77.9963 77.6970 77.4092 77.7322 78.0511 78.1530 NaN 78.8044 78.6020];
latlim=interp1(latlim,1:0.001:numel(latlim));
lonlim=[-111.2644 -109.8545 -110.1869 -112.0512 -113.5343 -112.7246 -111.2644 NaN -110.9637 -109.6631];
lonlim=interp1(lonlim,1:0.001:numel(lonlim));
Thanks!

I hope I understood your question:
If you want a constant step of 0.042 then try:
lonlim = interp1( lonlim, lonlim(1): 0.042 :lonlim(end) );
This will create a vector starting at the first value of lonlim, which is lonlim(1), with a step of 0.042, until the last value of lonlim, which is lonlim(end).
Hope it works! :)

Related

How do I add these intensity values per pixel in a fast way?

Just a quick question that is driving me mad due to its slowness.
I have a cell (300x2), the first column corresponds to the parallel intensity values, and the second one to the perpendicular intensity values. Each cell contains a 128x128 matrix.
So, what I want to do is summing all the intensities of all the matrixes, per pixel. I have the following code but it takes ages to be executed:
%Intensity_cell_par_per is the 300x2 cell
%a=128;
%b=128;
%numfiles=300;
I_matrix_par=zeros(128,128);
I_matrix_per=zeros(128,128);
for i=1:a
for j=1:b
I_par=0;
I_per=0;
for k=1:numfiles
I_par=I_par+sum(Image_cell_par_per{k,1}(i,j,1:end));
I_per=I_per+sum(Image_cell_par_per{k,2}(i,j,1:end));
end
I_matrix_par(i,j)=I_par;
I_matrix_per(i,j)=I_per;
end
end
Any idea of how I can do it in a fast way?
Thanks!
I am assuming here that the code you posted gives the result you want. This is an easy way of speeding it up:
You can call sum as sum( _ , 3) to take the whole matrix and sum its 3rd dimension, so you avoid 2 loops. The following code, run with a=100,b=100 and numfiles=20 runs in 0.0031 seconds as opposed to 5.17 seconds with your code, which is about 1660x faster. I hope this is fast enough.
I_matrix_par=zeros(a,b);
I_matrix_per=zeros(a,b);
for k=1:numfiles
I_matrix_par=I_matrix_par+sum(Image_cell_par_per{k,1},3);
I_matrix_per=I_matrix_per+sum(Image_cell_par_per{k,2},3);
end

How do I plot values in an array vs the number of times those values appear in Matlab?

I have a set of ages (over 10000 of them) and I want to plot a graph with the age from 20 to 100 on the x axis and then the number of times each of those ages appears in the data on the y axis. I have tried several ways to do this and I can't figure it out. I also have some other data which requires me to plot values vs how many times they occur so any advice on how to do this would be much appreciated.
I'm quite new to Matlab so it would be great if you could explain how things in your answer work rather than just typing out some code.
Thanks.
EDIT:
So I typed histogram(Age, 80) because as I understand that will plot the values in Age on a histogram split up into 80 bars (1 for each age). Instead I get this:
The bars aren't aligned and it's clearly not 1 per age nor has it plotted the number of times each age occurs on the y axis.
You have to use histogram(), and that's correct.
Let's see with an example.
I extract 100 ages between 20 and 100:
ages=randsample([20:100],100,true);
Now I call histogram() in this manner:
h=histogram(ages,[20:100]);
where h is an histogram object and this will also show the following plot:
However, this might look easy due to the fact that my ages vector is in range 20:100, so it will not contain any other values. If your vector, as instead, contains also ages not in range 20:100, you can specify the additional option 'BinLimits' as third input in histogram() like this:
h=histogram(ages,length([20:100]),'BinLimits',[20:100]);
and this option plots a histogram using the values in ages that fall between 20 and 100 inclusive.
Note: by inspecting h you can actually see and/or edit some proprieties of your histogram. An attribute (field) of such object you might be interested to is Values. This is a vector of length 80 (in our case, since we work with 80 bins) in which the i-th element is the number of items is the i-th bin. This will help you count the occurrences (just in case you need them to go on with your analysis).
Like Luis said in comments, hist is the way to go. You should specify bin edges, rather than the number of bins:
ages = randi([20 100], [1 10000]);
hist(ages, [20:100])
Is this what you were looking for?

Calculating vector lengths within my loop

The code is meant to draw a line with each click of the mouse made within the figure. Here is my code;
b=1
while b>0;
axis([-10, 10, -10, 10])
b=b+1
[x(b),y(b)]=ginput(1)
plot(x,y,x,y)
end
However I cant get my head around how I can add the vectors seeing as some are + and some are -, I need to turn the negatives into positives. I need to add code that will give me an overall combined length after all the mouse clicks. Maybe I am just thinking about it completely wrong.
I have tried;
length=(sqrt(x.^2)+(y.^2))
I was hoping this would give me the correct vector length accept unless I click an exact straight line.
So assuming that x and y are vectors of sequential points, if you want to get the total distance then you need to take the sum of the distance between each point i.e.
Σi(sqrt((xi-xi-1)2+(yi-yi-1)2))
in Matlab we can calculate all the x (and y) differences in one step using the diff function so in the end we get
length = sum(sqrt(diff(x).^2 + diff(y).^2))
Your problem is twofold: there's a typo, I think instead of
length=(sqrt(x.^2)+(y.^2))
you mean
length=sqrt((x.^2)+(y.^2))
The second problem is, that this does not actually calculate the length of your path, instead, you probably want something like
clear all
b=1;
radius = 3;
while b>0;
axis([-10, 10, -10, 10])
b=b+1;
[x(b),y(b)]=ginput(1);
plot(x,y,x,y);
length(b)=sqrt((x(b)-x(b-1))^2+(y(b)-y(b-1))^2);
if sqrt(x(b)^2 + y(b)^2) < radius; break; end
end
sum(length)
which calculates the length for each new piece you add and sums them all up.
As soon as you click within "radius" distance of 0 the while loop breaks.
Also, generally it's good practice to preallocate variables, no big deal here, cause your arrays are small, just saying.
Note: Dan's solution gives you a vectorized way of calculating the total length in one step, so in case you don't need the individual path lengths this is the more concise way to go.

Calculating value y for each XI and XII in MATLAB:

I am currently working in matlab to design a way to reconstruct 3D data. For this I have two pictures with black points. The difference in the amount of points per frame is key for the reconstruction, but MATLAB gives an error when matrixes are not equal. This is happening becaus the code is not doing what I want it to do, so can anyone hel me with the following?
I have two columns of Xdata: XLI and XRI
What matlab does when I do XLI-XRI is substracting the pairs i.e XLI(1)-XRI(1) etc, but I want to substract each value of XRI of every value of XLI. i.e
XLI(1)-XRI(1,2,3,4 etc)
XLI(2)-XRI(1 2 3 4 etc)
and so on
Can anyone help?
I think you are looking for a way to deduct all combinations from eachother. Here is an example of how you can do that with bsxfun:
xLI = [1 2 3]
xRI = [1 2]
bsxfun(#minus,xLI ,xRI')
I cannot comment on Dennis's post (not enough points on this website) : his solution should work, but depending on your version of Matlab you might get a "Error using ==> bsxfun" and need to transpose either xLI or xRI for that to work :
bsxfun(#minus,xLI' ,xRI)
Best,
Tepp

Random move of dots in a matrix in matlab

I have a matrix of zeros and ones. Each cell with a one value represents a non empty cell.
The cells with the ones is kept in a vector with the coordinates.
I'm iterating through the list and move each element randomly to one of its free neighboring cells.
Is there a way to do this with vector operations?
Thanks
Here's an attempt, not the most elegant or efficient one, but still it should work.
first I assume you have the x,y coordinates of your non empty cells, something like
c=[x y];
The relative positions of the 8 nearest neighbors (n.n.) in a 2D array are given by:
nn=[1 1;1 -1;-1 1;0 -1;-1 0;0 1;1 0;-1 -1];
Let's take all possible permutation of each x,y coordinate in c around its n.n.
permc=bsxfun(#plus,repmat(c,[1 8]),nn(:)');
Now set a vector of random n.n. out of the 8 options for each x,y coordinates:
ri=randi(8,numel(x), 1);
and use it to select random new coordinates
new_c= [ permc(sub2ind(size(permc), (1:numel(x))', 2*ri-1 )) , ...
permc(sub2ind(size(permc), (1:numel(x))', 2*ri))];
Issues:
The code doesn't check if there is a n.n. that is not free, not hard to solve having info of c and permc at hand.
The code doesn't care that a point is on the edge of the array, so it can select a coordinate that is either 0 or one larger than the size of the array. Again, permc has the info to tackle this case and it can be treated separately in several ways.
That's the jist.
Probably, but I think it would be quite tricky to write. You'd have to be careful to prevent two "dots" from moving into the same empty location at the same time.
Looping over the vector and dealing with one dot at a time seems far easier. What's your reason for wanting to do this with vector operations? If you're looking to make your code faster, post your code and ask for suggestions.