Creating a 2D array in IDL starting from an equation - arrays

I'm trying to make a contour plot in IDL of quantity described by and equation, which here I'll take to be x^2 + y.
In order to do that, I first need to create a 2D array ("pxx").
Being a novice, I'm currently just moving my fist step into this direction and so far I've been trying to make this simpler foreach loop work:
pxx=fltarr(10, 10)
xx = indgen(10)
yy = indgen(10)
foreach k, xx do begin
pxx[k,*]=3*k
endforeach
print, pxx
But this only seems to work for the last column. Any idea on how to fix that? And how would you suggest I proceed to create a 2D array in space for the equation above?
Thank you in advance, any help is appreciated

Choose the range of x and y values you want to evaluate on:
n = 10
x = findgen(n) - (n - 1)/2.0
y = findgen(n) - (n - 1)/2.0
Expand x and y to 2-dimensional versions of themselves:
xx = rebin(reform(x, n, 1), n, n)
yy = rebin(reform(y, 1, n), n, n)
Evaluate the function:
z = xx^2 + yy
Plot:
contour, z, x, y

Related

Bootstrapping of pairs in Matlab

I would like to extend "univariate bootstrapping" to "multivariate bootsrapping", meaning in a first step I draw randomly with replacement out of a one-dimensional vector using this code:
s = RandStream.getGlobalStream();
reset(s)
n = 100000; % # of independent random trials
h = 52; % horizon
T = size(Resid_standard, 1);
Resid_bootstrapped = Resid_standard(unidrnd(T, h, n));
Now, the basic vector Resid_standard is not a uni-dimensional vector but a Tx2 matrix and I want to not only draw random numbers but random pairs.
How do I have to modify my code to achieve this?
The output in the univariate case is a 100000x50 matrix. The output for the two-dimensional case would be three-dimensional. How could I store my results?
One solution is to store the index vector, make use of linear indexing, and concatenate the results:
r_ind = unidrnd(T, h, n);
Resid_bootstrapped = cat(3, Resid_standard(r_ind), Resid_standard(r_ind + T));
Resid_bootstrapped will then be a h×n×2 matrix.
This can even be shortened into a one-liner:
Resid_bootstrapped = reshape(Resid_standard(unidrnd(T, h, n), [1,2]), h, n, 2);

Value error in drawing the contourf plot

I want to draw the contourf of a certain function and my code was as follows:
xlist = linspace(0, 100, 100)
ylist = linspace(0, 100, 200)
X, Y = meshgrid(xlist, ylist)
#print "X = " + str(X)
#print "Y = " + str(Y)
Z = power_at_each_point(X, Y)
#print "Z = " + str(Z)
figure()
CP2 = contourf(X, Y, Z)
colorbar(CP2)
title('Contour Plot')
xlabel('Room-x (m)')
ylabel('Room-y (m)')
show()
The function power_at_each_point(X,Y) when I test it alone I write:
print power_at_each_point(50, 50)
and the output is -80.9187477018
which basically represents the power reached to this point in the room and it outputs a number normally but when I call it after the meshgrid command it returns an error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I want to take each coordinate of points in the room x-coord and y-coord and calculate the power reached at this point using the power_at_each_point method
which is supposed to return a number and I'd represent it in the contourf plot.
My guess is that the arguments (X,Y) of Z = power_at_each_point changed from being just numbers to being arrays which I don't want and that's what is causing the error.
How can I let the function Z = power_at_each_point(X,Y) take as arguments X as a number ex :1 and Y ex :2 and return a value for the power at this point so that I can represent it in my contourf plot.
Any help would be appreciated.
I've found that the function meshgrid wants a Matrix pretty much as an argument so I went ahead and created a Matrix called Z and I filled by myself the values in it and then I went ahead and entered Z as an argument to the meshgrid function:
x_list = linspace(0, 100, 100)
y_list = linspace(0, 100, 100)
X, Y = meshgrid(x_list, y_list)
Z = [[0 for x in range(len(x_list))] for x in range(len(y_list))]
for each_axes in range(len(Z)):
for each_point in range(len(Z[each_axes])):
Z[each_axes][each_point] = power_at_each_point(each_axes, each_point)
figure()
CP2 = contourf(X, Y, Z)
and that got me the result I wanted as far as I was asking here.
The points is reversed in the Z Matrix or as you can say mirrored along the horizontal but that's something i'm gonna play with so that I can get those elements in Z to match how the meshgrid actually sets it's grid points.

Skip some columns when searching a matrix in a loop

I have a matrix, c, and I want to search many times for the index of the positive minimum element
d = min(c(c>0));
[x,y] = find(c == d);
but in the next search I want it to skip the old y.
how to do it?
I want to use x and y in some other calculation.
also I want to find this d minimum just within specific columns in the matrix c like:
j from m+1 to n-1
please help
Define mask = zeros(size(c)); before the loop.
And before finding the minimum use,
newc = c + mask;
d = min(newc(newc>0));
[x,y] = find(newc == d);
mask(:,y) = NaN;
I think you can update the c matrix. I mean:
% In the loop, use it:
[x,y]=find(c==d);
c(:, y) = [];
If c matrix is important, you can use a temporary variable equals to c, instead of using c.

How to get an ndgrid from a matrix?

V is a 3D matrix with uniformly spaced voxels. A way to get a coordinate grid / meshgrid would be:
[x y z] = ndgrid( 1:size(V,1), 1:size(V,2), 1:size(V,3) );
which feels redundant, especially if the number of dimensions is even higher. Is there a neater way to do this?
I guess you could do it like this is you have a lot of dimensions (or a changing number of dimensions):
C = arrayfun(#(x)(1:size(V,x)),1:ndims(V),'UniformOutput',false);
[outArgs{1:ndims(V)}] = ndgrid(C{:})
So now using your example
outArgs{1} == x;
outArgs{2} == y;
outArgs{3} == z;
But for 3 dimensions, either leave it has you have it or else maybe you'll find this neater:
[m, n, p] = size(V);
[x, y, z] = ndgrid(1:m, 1:n, 1:p);

store data in array from loop in matlab

I want to store data coming from for-loops in an array. How can I do that?
sample output:
for x=1:100
for y=1:100
Diff(x,y) = B(x,y)-C(x,y);
if (Diff(x,y) ~= 0)
% I want to store these values of coordinates in array
% and find x-max,x-min,y-max,y-min
fprintf('(%d,%d)\n',x,y);
end
end
end
Can anybody please tell me how can i do that. Thanks
Marry
So you want lists of the x and y (or row and column) coordinates at which B and C are different. I assume B and C are matrices. First, you should vectorize your code to get rid of the loops, and second, use the find() function:
Diff = B - C; % vectorized, loops over indices automatically
[list_x, list_y] = find(Diff~=0);
% finds the row and column indices at which Diff~=0 is true
Or, even shorter,
[list_x, list_y] = find(B~=C);
Remember that the first index in matlab is the row of the matrix, and the second index is the column; if you tried to visualize your matrices B or C or Diff by using imagesc, say, what you're calling the X coordinate would actually be displayed in the vertical direction, and what you're calling the Y coordinate would be displayed in the horizontal direction. To be a little more clear, you could say instead
[list_rows, list_cols] = find(B~=C);
To then find the maximum and minimum, use
maxrow = max(list_rows);
minrow = min(list_rows);
and likewise for list_cols.
If B(x,y) and C(x,y) are functions that accept matrix input, then instead of the double-for loop you can do
[x,y] = meshgrid(1:100);
Diff = B(x,y)-C(x,y);
mins = min(Diff);
maxs = max(Diff);
min_x = mins(1); min_y = mins(2);
max_x = maxs(1); max_y = maxs(2);
If B and C are just matrices holding data, then you can do
Diff = B-C;
But really, I need more detail before I can answer this completely.
So: are B and C functions, matrices? You want to find min_x, max_x, but in the example you give that's just 1 and 100, respectively, so...what do you mean?

Resources