I have an array like this:
>>>Opt
array([[array([[0.5]])]], dtype=object)
How to remove these brackets and get the value of 0.5 as a single floating point?
I have tried
>>>np.array(Opt)
array([[array([[0.5]])]], dtype=object)
>>>Opt.ravel()
array([[array([[0.5]])]], dtype=object)
>>>Opt.flatten()
array([[array([[0.5]])]], dtype=object)
None of these works. Is it because of the data type "object"?
That is a 4-dimensional numpy array you defined, so in this instance to basic way to get the number you have to navigate the four dimensions:
import numpy as np
the_array = np.array([[np.array([[0.5]])]], dtype=object)
print(the_array[0][0][0][0])
Output:
0.5
I don't know what you want to do with this array, based on your use case there may be better approaches to your problem.
dtype=object means that you defined an array of pointers to Python objects, this defines both the way memory is managed when allocating space for the array and the permitted operations on the elements.
I find out the best way to do it is using item().
Opt.item().item()
Related
Very simple question, which I have looked for but haven't found a clear answer. I would like to efficiently apply a mapping to an input ndarray, remapping each element in the array and returning the modified array.
A simple version using numpy would be something like:
def remap(a, mapping):
return np.vectorize(mapping.__getitem__)(a)
I am pretty sure I can't use Numba vectorize but was hoping to be able to use guvectorize. But it looks like I can't pass a Numba TypedDict into a guvectorize function either. Any suggestions welcome
If you know the number of dimentions of the target array, then you just need a simple loop. AFAIK, guvectorize does not support this use-case yet. However, you can solve this using a simple reshape.
import numba as nb
#nb.njit
def remap(a, mapping):
# Linearize the array (can cause a copy of `a`)
b = a.reshape(-1)
for i in range(b.size):
b[i] = mapping[b[i]]
return b.reshape(a.shape)
Note that the first reshape can cause a copy of a if a is not contiguously store in memory and Numpy cannot find a unified stride in 1D. Thus, the returned array may be a copy of a. The second reshape is guaranteed not to copy the array. If you want to always return a copy of the input array (no mutation) like most Numpy function, then you can use flatten instead of the first reshape call.
I'd like to understand why the following code works:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b{1}=frame2im(a);
instead the following code does not work:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b(1)=frame2im(a);
If I use "b(1)=x;" it works.
Thank you very much.
In an array, you can store only one 1x1 value of any class at a single index but the class of all elements of an array must be same. In a cell array, there is no such restriction.
frame2im(a) is [525x700x3 uint8] and hence you can store it in a cell and not in a simple array if you want to store it at a single index.
b(1)=x; works because x is 1x1 matlab.ui.Figure. You can also store x in a cell array.
To my understanding, you need to know what cells are meant for in MATLAB. If you happen to know Python, you probably will think in a "list"-type way. MATLAB cell can store numbers, strings, etc. However its array is meant to store numbers.
That's why your structure from fram2im can't work.
Hello and thanks for the help,
I have seen that you can make this statement:
some_variable = []
from what I see, this makes like a void variable for later use.
The thing is I want to make it an array (and that is the problem). I made this
some_variable(:) = []
but there is an error:
In an assignment A(:) = B, the number of elements in A and B must be the same
Thanks
Actually everything in MATLAB is an array, sometimes the arrays are just 1x1, but they are arrays nevertheless (unlike in C/C++ where you have int or int*).
So, when you do var=[] you initialize an empty array, an array of size 0*.
After that, its up to you to initialize it with whatever size you want. var=0 will initialize it as an array of 1x1 size, but you can go bigger, using zeros(size).
Adittionally, if you want to create empty variables of another class, just use <classname>.empty, such as in var=uint32.empty;
*Note that in MATLAB, an array is also infinite dimensional. It's not size 0, its 0x0x0x0x0x.....x0x0. If you want to prove this, try size(var,999999999999) in MATLAB.
I have a cell array containing double arrays like:
x = {[4,1] [4,3] [1,1] [2,3] [2,1]};
I would like to check if [1,1] is contained in the cell array and if so, delete it. I get it done like this:
x(find(cellfun(#all,cellfun(#(x)x==[1,1],x(:),'UniformOutput', false))==1)) = []
Seems overy complicated though, any suggestions for simplification? thanks in advance!
Without using cellfun, one can use ismember to detect the matching rows and remove them -
x(ismember(vertcat(x{:}),[1 1],'rows'))=[]
Basically the same code you used, but removing everything unnecessary. You don't need to apply cellfun twice to apply two nested functions. Pass the nested function instead.
x(cellfun(#(x)all(x==[1,1]),x)=[]
Besides this, take a look at "logical indexing", you don't need find in such cases.
What is the syntax for declaring a global 2-dimensional array in MATLAB?
I want the array to be blank, or uninitialized. That is, I want to be able to initialize it to some value later on using a for-loop. All the examples I have come across so far consist of initializing the array when it is declared. I find this rather tedious because my array might have to be a large one.
Thanks.
Declare a variable as global first before using it:
global my_glob_var;
MATLAB doesn't really support the concept of 'uninitialised' variables, but you can create an array of NaNs (not a number) to indicate that each value hasn't been assigned yet. The arguments to the nan function indicate the size of the NaN array you wish to create:
my_glob_var = nan(200, 200)
There are other similar functions in case you wish to initialise arrays of zeros, ones, Inf etc.
Then inside the functions you want to use it in, declare it as global again:
function my_function
global my_glob_var % allows this function to use the global variable
my_glob_var % outputs the variable to command
As an aside, you note that you will "initialise it to some value later using a for-loop". Depending on how you are initialising the array, there may be a vectorised way to achieve this (i.e. without using a for-loop). Vectorised operations are usually much quicker in MATLAB.