np.where() with np.linspace() - numpy-ndarray

I am trying to find index of an element in x_norm array with np.where() but it doesn' t work well.
Is there a way to find index of element?
x_norm = np.linspace(-10,10,1000)
np.where(x_norm == -0.19019019)
Np.where works with np.arange() and can find the index either first or last element of array creating by linspace.

The numbers generated by np.linspace contains more decimal places than the one you are pasting to np.where (-0.19019019019019012).
So it might be better to use np.argmin to find the nearest value and avoid rounding errors:
x_norm = np.linspace(-10,10,1000)
yournumber=-0.19019019
idx=np.argmin(np.abs(x_norm-yournumber))
You can then go further and add np.where(x_norm==x_norm[idx]) to your code in case if you'll have array with duplicates.

Set the level of precision to 8 using np.round then use np.where to filter data as a mask then apply the mask to the array.
x_norm = np.round(np.asarray(np.linspace(-10,10,1000)),8)
results=x_norm[np.where(x_norm==-9.91991992)]
print(results)

Related

How to grab Index instead of relative position using xpath

Given the following xml:
<randomName>
<otherName>
<a>item1</a>
<a>item2</a>
<a>item3</a>
</otherName>
<lastName>
<a>item4</a>
<a>item5</a>
</lastName>
</randomName>
Running: '//a' Gives me an array of all 5 "a" elements, however '//a[1]' does not give me the first of those five elements (item1). It instead gives me an array containing (item1 and item 4).
I believe this is because they are both position 1 relatively. How can I grab any a element by its overall index?
I would like to be able to use a variable "x" to get itemX.
You can wrap it in parenthesis so it knows to apply the index to the entire result set
(//a)[1]

How to get an array of values where columns match multiple criteria

I have a table of data similar to:
where I'd like to get just the shapes which match a set of given criteria (in this case week=2 and colour=blue).
I can return the first result using index and match like:
=ArrayFormula(INDEX(C2:C14,MATCH($F$1&$F$2,A2:A14&B2:B14,0)))
but I'd like to return the all matching values (eg square and triangle) in to the range F3:Fsomething. This would preferably be done using a formula that returns a range and isn't "copied-down", as a list of all possible shapes isn't known beforehand.
How can I modify this formula to achieve this?
See if this works:
=FILTER (C2:C14, B2:B14=F2, A2:A14=F1)
to do multiple criteria you want to use * like so
=FILTER (C2:C14, (B2:B14=F2) * (A2:A14=F1))
and if you want the results all in the same cell with a delimiter, use TEXTJOIN
=TEXTJOIN([DELIMETER],[IGNORE EMPTY TEXT],text1)
=TEXTJOIN(", ",TRUE,FILTER(C2:C14,(B2:B14=F2)*(A2:A14=F1)))

Matlab : image region analyzer. Alternative for 'bwpropfilt'?

I'm running basic edge detection to detect windows region based on this http://www.mathworks.com/videos/edge-detection-with-matlab-119353.html
The edge works successfully :
final_edge = edge(gray_I,'sobel');
BW_out = bwareaopen(imfill(final_edge,'holes'),20);
figure;
imshow(BW_out);
Now when come to these following codes to filter image based on properties, it seems like my MATLAB R2013a can't identify this bwpropfilt method.
% imageRegionAnalyzer(BW);
% Filter image based on image properties
BW_out = bwpropfilt(BW_out,'Area', [400, 467]);
BW_out = bwpropfilt(BW_out,'Solidity',[0.5, 1]);
It says:
Undefined function 'bwpropfilt' for input arguments of type 'char'.
Then what should be my alternative to change this bwpropfilt?
bwpropfilt simply takes a look at the corresponding attribute that is output from regionprops and gives you objects that conform to that certain range and also filtering out those that are outside of the range. You can rewrite the algorithm by explicitly calling regionprops, creating a logical array to index into the structure to retain only the values within the right range (seen in the third input of bwpropfilt) corresponding to the property you want to examine (seen in the second input of bwpropfilt). If you want to finally reconstruct the image after filtering, you'll need to use the column major linear indices found in the PixelIdxList attribute, stack them all into a single vector and write to a new output image by setting all of these values to true.
Specifically, you can use the following code to reproduce the last two lines of code you have shown:
% Run regionprops and get all properties
s = regionprops(BW_out, 'all');
%%% For the first line of code
values = [s.Area];
s = s(values > 400 & values < 467);
%%% For the second line of code
values = [s.Solidity];
s = s(values > 0.5 & values < 1);
% Stack column major indices
ind = vertcat(s.PixelIdxList);
% Create output image
final_out = false(size(BW_out));
final_out(ind) = true;
final_out contains the filtered image only retaining the values within the range specified by the desired property.
Caution
The above logic only works for attributes returned from regionprops that contain only a single scalar value per unique region. If you examine the supported properties found in bwpropfilt, you will see that this list is a subset of the full list found in regionprops. This makes sense as certain regionprops properties return a vector or a matrix depending on what you choose so using a range to filter out properties becomes ambiguous if you have multiple values that characterize a particular unique region returned by regionprops.
Minor Note
Being curious, I opened up bwpropfilt to see how it is implemented as I currently have MATLAB R2016a. The above logic, with the exception of some exception handling, is essentially how bwpropfilt has been implemented so the code that I wrote is in line with the logic of the function.

how can I get the location for the maximum value in fortran?

I have a 250*2001 matrix. I want to find the location for the maximum value for a(:,i) where i takes 5 different values: i = i + 256
a(:,256)
a(:,512)
a(:,768)
a(:,1024)
a(:,1280)
I tried using MAXLOC, but since I'm new to fortran, I couldn't get it right.
Try this
maxloc(a(:,256:1280:256))
but be warned, this call will return a value in the range 1..5 for the second dimension. The call will return the index of the maxloc in the 2001*5 array section that you pass to it. So to get the column index of the location in the original array you'll have to do some multiplication. And note that since the argument in the call to maxloc is a rank-2 array section the call will return a 2-element vector.
Your question is a little unclear: it could be either of two things you want.
One value for the maximum over the entire 250-by-5 subarray;
One value for the maximum in each of the 5 250-by-1 subarrays.
Your comments suggest you want the latter, and there is already an answer for the former.
So, in case it is the latter:
b(1:5) = MAXLOC(a(:,256:1280:256), DIM=1)

jqGrid permutation array

In jqGrid I am trying to use the permutation array for saving the reorder state of the columns.
For eg. Basic column state is perm = [0,1,2,3,4] column 3 is hidden and column 0 is the checkbox. Now I have a custom context menu which I use to finally give me a perm array of [0,1,3,2,4]
I have read in the documentation that the permutation array needs to start with 1, is this right?
When I try using "remapColumns" functions of the jqgrid and pass the perm array, it works fine. But if I try hiding and showing columns a couple of times, the column order is getting messed with.
Please help me understand what these indices for the permutation array stand for? Are they column indexes for visible columns? Should hidden columns be part of array? What happens in case of frozen columns? In some examples I have seen perm = [0:1, 1:3, 2:2, 3:1]
What is the correct way? I am using grid.jqGrid("remapColumns", perm, true);
Try to use also the last parameter of the function
grid.jqGrid("remapColumns", [0,1,3,2,4], true, false);
permutation, updateCells, keepHeader
wiki:methods

Resources