Initialize array without zeroes - arrays

I have an 3 dimensional array that represents an xy grid, and the z vector represents depth. I only know depths of certain rows and am trying to interpolate the array. My questions is how do I create a 720x400 array, without setting all the values to 0 (as that could affect the interpolation).
Thanks!

You can use:
A = nan(m,n,...);
to initialize a matrix with NaN's, if that is what you ask for. Other popular choices are inf(m,n,...) to initialize with Inf's and ones(m,n,...) to initialize with 1's.
So, to create a 720x400 matrix full of NaN's you can just:
A = nan(720,400);

It is not necessary to initialize the empty rows to a special value. Instead, you can modify the interpolation procedure to assign a zero weight to these rows. Then, they will not affect the interpolation.
A simple way to do so in MATLAB would be to use the griddata method for the interpolation.

Related

how to make an array of homogeneous data value in array?

I want to create a 3x3 array filled with tens.
I know for array filled with zeros or ones, i can use np.zeros() and np.ones(), respectively.
But what about an array filled with tens?
Are there no special ways to do this?
You can use np.fill() to fill an array with any value.

Excel: creating an array with n times a constant

I have been looking around for a while but unable to find an answer to my question.
In Excel, what compact formula can I use to create an array made up of a single element repeated n times, where n is an input (potentially hard-coded)?
For example, something that would look like this (the formula below does not work but gives an idea of what I am looking for):
{={"Constant"}*3}
Note: I am not looking for a VBA-based solution.
EDIT Reading #AxelRichter answer, I see I should also indicate that the formulas below assume Constant is a number. If Constant is text, then this solution will not work.
Volatile:
=ROW(INDIRECT("1:" & Repts))/ROW(INDIRECT("1" & ":" & Repts)) * Constant
non-Volatile:
=ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,Repts,1))/ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,Repts,1))*Constant
If
Constant = 14
Repts = 3
then
Result = {14;14;14}
The first part of the formulas create an array of 1's repeated Repts times. Then we multiply that array by Constant to get the desired result.
And after reading #MacroMarc's comment, the following non-volatile formula shouyld also work for numbers:
=(ROW($A$1:INDEX($A:$A,Repts))>0)*Constant
One could concatenate 1:n empty cells to the "Constant" to create a string array having n items "Constant":
"Constant"&INDEX(XFD:XFD,1):INDEX(XFD:XFD,3)
There 3 is n.
Used in Formula
=INDEX("Constant"&INDEX(XFD:XFD,1):INDEX(XFD:XFD,3),0)
Evaluate Formula shows that it works:
Here column XFD is used because in most cases this column will be empty and a column which is guaranteed to be empty is needed for this solution.
If used
"Constant"&T(ROW($A$1:INDEX($A:$A,3)))
=INDEX("Constant"&T(ROW($A$1:INDEX($A:$A,3))),0)
the need of an empty column disappears. The function ROW returns numbers but the T returns an empty string if its parameter is not text. So empty strings will be concatenated for each 1:3 (n).
Thanks to #MacroMarc for the hint.
Try:
REPT("Constant", SEQUENCE(3,1,1,0))
Or, if the reference is to a dynamic array:
REPT("Constant", SEQUENCE(A1#,1,1,0))
The dynamic array spills, and has your constant repeated one time.
Using SEQUENCE with a step of 0 is a much cleaner way to make an array of constants. You can choose whether you want rows or columns (or both!) as well.
=SEQUENCE(Repts,1,Constant,0)
I will generally use a sequence (like Claire (above) said). But if you want to provide an output of text objects, I would do it this way:
=IF(SEQUENCE(A1,A2,1,0),A3)
Where:
A1 has the number of rows
A2 has the number of columns
A3 has the thing you want repeated into an array
The sequence will create a matrix of 1's, which the IF statement will default to the TRUE expression (being the contents of A3).
So, if you wanted a vertical list of 3 items that says "Constant", this would do it:
=IF(SEQUENCE(3,,1,0),"Constant")
If you would prefer it be arranged horizontally instead of vertically, just amend the SEQUENCE function:
=IF(SEQUENCE(,3,1,0),"Constant")

Make 2-D numpy arrays from regular grid without excess memory use

Many of the plotting functions I use to visualize data (plot_surface, plot_wireframe, mayavi's contour3, etc.) take as arguments 2-D arrays X, Y, Z, and some scalar value of the function.
I usually have information from a file in the format
x y z data
0 0 1 45
...
Which is on a regular grid. I have way too many values to be able to hold the output from meshgrid in my memory, but I can hold the full dataset as either an Nx4 or four Nx1 arrays.
Is there a way to make a view, or restructure the existing gridded data to a format that is compatible with these functions?
I realize that I can use griddata and interpolate to lower the resolution, and that is my current approach.
UPDATE:
The specific task I'm working on uses mayavi's contour3 function to generate an isosurface plot, but the approach of multidimensionalizing the arrays should be general.
You can greatly reduce memory use by passing copy=False to meshgrid. This creates views into the original arrays, see the docs

signrank test in a three-dimensional array in MATLAB

I have a 60x60x35 array and would like to calculate the Wilcoxon signed rank test to calculate if the median for each element value across the third array dimension (i.e. with 35 values) is different from zero. Thus, I would like my results in two 60x60 arrays - with values of 0 and 1 depending on the test statistic, and in a separate array with corresponding p values.
The problem I am facing is specifying the command in a way that desired output would have appropriate dimensions and would be calculated across the appropriate dimension of the array.
Thanks for your help and all the best!
So one way to solve your problem is using a nested for-loop. Lets say your data is stored in data:
data=rand(60,60,35);
size_data=size(data);
p=zeros(size_data(1),size_data(2));
p(:,:)=NaN;
h=zeros(size_data(1),size_data(2));
h(:,:)=NaN;
for k=1:size_data(1)
for l=1:size_data(2)
tmp_data=data(k,l,:);
tmp_data=reshape(tmp_data,1,numel(tmp_data));
[p(k,l), h(k,l)]=signrank(tmp_data);
end
end
What I am doing is I preallocate the memory of p,h as a 60x60 matrix. Then I set them to NaN, so if you can easily see if sth went wrong (0 would be an acceptable result). Now I loop over all elements and store the actual data array in a new variable. signrank needs the data to be an array so I reshape it to two dimensions.
I guess you could skip those loops by using bsxfun

Problem with assigning elements of a class array to individual variables in MATLAB

This is a bit of a duplicate of this question, this question, and this question, however those solutions don't work, so I'm asking mine.
I've got an array of locally defined classes and I'd like to assign it to multiple, individual variables. This pattern doesn't work:
%a is 2x1 of MyClass
temp = mat2cell(a);
[x,y] = temp{:};
%throws:
??? Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Because temp is a single cell, with my 2x1 array in one cell, rather than a 2x1 cell array with one element of each of my original array in one cell.
Any ideas?
You should use the function NUM2CELL instead of the function MAT2CELL in order to place each element of your array a in a separate cell of your cell array temp.
Using MAT2CELL with just one input is equivalent to doing temp = {a};, and in my version of MATLAB (R2009a) I actually get this warning:
>> temp = mat2cell(a);
Warning: Single input behavior is obsolete and will be removed in a
future release of MATLAB. Use C={X} instead.
> In mat2cell at 54

Resources