Matrix Math with VBA (System of Linear Equations) - arrays

I'm looking for a little help performing some matrix mathematics in Excel's VBA. I've read a ton of replies that suggest using the Excel worksheet but I'm hoping to solve this within the VBA code itself.
Although my application is much larger, let's say I have a system of linear equations that I need to solve:
x1 + x2 = 8
2*x1 + 4*x2 = 100
This can be solved with the simple matrix formula A*x = B or x = A^(-1) * B where,
A = [1, 1; 2, 4]
B = [8; 100]
If you solve this, you'll find x1 = -34 and x2 = 42. In terms of the matrix, then:
X = [-34; 42]
Using Excel's worksheets alongside its MMULT and MINVERSE functions makes this easy and I've gotten it to work just fine. My problem is I'm needing to do this calculation inside a VBA function. Here's what I'm trying:
Dim A(0 To 1, 0 To 1) As Single
Dim B(0 To 0, 0 To 1) As Single
Dim X(0 To 0, 0 To 1) As Single
A(0, 0) = 1
A(1, 0) = 1
A(0, 1) = 2
A(1, 1) = 4
B(0, 0) = 8
B(0, 1) = 100
X = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MInverse(A), B)
Unfortunately, the last line yields a "Compile error: can't assign to array" message. I think it's because I have to specify each element of the array one at a time, but the worksheet functions are array functions.
How do I fix this?

Two things:
The same rule applies as in actual mathematics: B must be a vertical array for matrix multiplication to be possible in your case. Declare it as
Dim B(0 To 1, 0 To 0) As Single
and initialize it accordingly. Also, just declare
Dim X As Variant
since the MMult returns a Variant array. This is what was causing your original error.

Related

VBA: Return the closest value using a 1-Dimensional Array

I am using the WorksheetFunction.Large and WorksheetFunction.CountIf commands to determine the closest "jaw size" using a 1-Dimensional array as the source data, shown below.
wsSheet.Range("H2").Value = WorksheetFunction.Large(myArray, WorksheetFunction.CountIf(myArray, ">" & SizePush) + 1)
The problem I am having is when I use whole numbers (1, 2, 3, 4) the resulting jaw size does not take the closest value from the array, it takes the second closest value. The array I am using is shown in image 1 (myArray), and 'SizePush' refers to the following equation: (Start Diameter - (Start Diameter - End Diameter))-0.05.
a snippet of the jaw size array
I have attached the code that I am using. If anyone can help that would be greatly appreciated because I cannot figure out why only whole numbers cause an issue.
Dim StartDiam, EndDiam, PReduction, Push1, Push2, Push3, Push4, SizePush
StartDiam = 0.5
EndDiam = 4.75
PReduction = Worksheets("Sheet1").Range("D2").Value
Push1 = Worksheets("Sheet1").Range("I2").Value
Push2 = Worksheets("Sheet1").Range("I3").Value
Push3 = Worksheets("Sheet1").Range("I4").Value
Push4 = Worksheets("Sheet1").Range("I5").Value
SizePush = Worksheets("Sheet1").Range("I6").Value
Dim myArray
Set myArray = Range("T2:T51")
Dim wsSheet As Worksheet
Set wsSheet = Worksheets("Sheet1")
If StartDiam < wsSheet.Range("B2").Value Then
If EndDiam > wsSheet.Range("C2").Value Then
'size of jaw if the push is one
If wsSheet.Range("I2").Value = Push1 Then
wsSheet.Range("H2").Value = WorksheetFunction.Large(myArray, WorksheetFunction.CountIf(myArray, ">" & SizePush) + 1)
Exit Sub
End If

In MATLAB how can I write out a multidimensional array as a string that looks like a raw numpy array?

The Goal
(Forgive me for length of this, it's mostly background and detail.)
I'm contributing to a TOML encoder/decoder for MATLAB and I'm working with numerical arrays right now. I want to input (and then be able to write out) the numerical array in the same format. This format is the nested square-bracket format that is used by numpy.array. For example, to make multi-dimensional arrays in numpy:
The following is in python, just to be clear. It is a useful example though my work is in MATLAB.
2D arrays
>> x = np.array([1,2])
>> x
array([1, 2])
>> x = np.array([[1],[2]])
>> x
array([[1],
[2]])
3D array
>> x = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
>> x
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
4D array
>> x = np.array([[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]])
>> x
array([[[[ 1, 2],
[ 3, 4]],
[[ 5, 6],
[ 7, 8]]],
[[[ 9, 10],
[11, 12]],
[[13, 14],
[15, 16]]]])
The input is a logical construction of the dimensions by nested brackets. Turns out this works pretty well with the TOML array structure. I can already successfully parse and decode any size/any dimension numeric array with this format from TOML to MATLAB numerical array data type.
Now, I want to encode that MATLAB numerical array back into this char/string structure to write back out to TOML (or whatever string).
So I have the following 4D array in MATLAB (same 4D array as with numpy):
>> x = permute(reshape([1:16],2,2,2,2),[2,1,3,4])
x(:,:,1,1) =
1 2
3 4
x(:,:,2,1) =
5 6
7 8
x(:,:,1,2) =
9 10
11 12
x(:,:,2,2) =
13 14
15 16
And I want to turn that into a string that has the same format as the 4D numpy input (with some function named bracketarray or something):
>> str = bracketarray(x)
str =
'[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]]'
I can then write out the string to a file.
EDIT: I should add, that the function numpy.array2string() basically does exactly what I want, though it adds some other whitespace characters. But I can't use that as part of the solution, though it is basically the functionality I'm looking for.
The Problem
Here's my problem. I have successfully solved this problem for up to 3 dimensions using the following function, but I cannot for the life of me figure out how to extend it to N-dimensions. I feel like it's an issue of the right kind of counting for each dimension, making sure to not skip any and to nest the brackets correctly.
Current bracketarray.m that works up to 3D
function out = bracketarray(in, internal)
in_size = size(in);
in_dims = ndims(in);
% if array has only 2 dimensions, create the string
if in_dims == 2
storage = cell(in_size(1), 1);
for jj = 1:in_size(1)
storage{jj} = strcat('[', strjoin(split(num2str(in(jj, :)))', ','), ']');
end
if exist('internal', 'var') || in_size(1) > 1 || (in_size(1) == 1 && in_dims >= 3)
out = {strcat('[', strjoin(storage, ','), ']')};
else
out = storage;
end
return
% if array has more than 2 dimensions, recursively send planes of 2 dimensions for encoding
else
out = cell(in_size(end), 1);
for ii = 1:in_size(end) %<--- this doesn't track dimensions or counts of them
out(ii) = bracketarray(in(:,:,ii), 'internal'); %<--- this is limited to 3 dimensions atm. and out(indexing) need help
end
end
% bracket the final bit together
if in_size(1) > 1 || (in_size(1) == 1 && in_dims >= 3)
out = {strcat('[', strjoin(out, ','), ']')};
end
end
Help me Obi-wan Kenobis, y'all are my only hope!
EDIT 2: Added test suite below and modified current code a bit.
Test Suite
Here is a test suite to use to see if the output is what it should be. Basically just copy and paste it into the MATLAB command window. For my current posted code, they all return true except the ones more than 3D. My current code outputs as a cell. If your solution output differently (like a string), then you'll have to remove the curly brackets from the test suite.
isequal(bracketarray(ones(1,1)), {'[1]'})
isequal(bracketarray(ones(2,1)), {'[[1],[1]]'})
isequal(bracketarray(ones(1,2)), {'[1,1]'})
isequal(bracketarray(ones(2,2)), {'[[1,1],[1,1]]'})
isequal(bracketarray(ones(3,2)), {'[[1,1],[1,1],[1,1]]'})
isequal(bracketarray(ones(2,3)), {'[[1,1,1],[1,1,1]]'})
isequal(bracketarray(ones(1,1,2)), {'[[[1]],[[1]]]'})
isequal(bracketarray(ones(2,1,2)), {'[[[1],[1]],[[1],[1]]]'})
isequal(bracketarray(ones(1,2,2)), {'[[[1,1]],[[1,1]]]'})
isequal(bracketarray(ones(2,2,2)), {'[[[1,1],[1,1]],[[1,1],[1,1]]]'})
isequal(bracketarray(ones(1,1,1,2)), {'[[[[1]]],[[[1]]]]'})
isequal(bracketarray(ones(2,1,1,2)), {'[[[[1],[1]]],[[[1],[1]]]]'})
isequal(bracketarray(ones(1,2,1,2)), {'[[[[1,1]]],[[[1,1]]]]'})
isequal(bracketarray(ones(1,1,2,2)), {'[[[[1]],[[1]]],[[[1]],[[1]]]]'})
isequal(bracketarray(ones(2,1,2,2)), {'[[[[1],[1]],[[1],[1]]],[[[1],[1]],[[1],[1]]]]'})
isequal(bracketarray(ones(1,2,2,2)), {'[[[[1,1]],[[1,1]]],[[[1,1]],[[1,1]]]]'})
isequal(bracketarray(ones(2,2,2,2)), {'[[[[1,1],[1,1]],[[1,1],[1,1]]],[[[1,1],[1,1]],[[1,1],[1,1]]]]'})
isequal(bracketarray(permute(reshape([1:16],2,2,2,2),[2,1,3,4])), {'[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]]'})
isequal(bracketarray(ones(1,1,1,1,2)), {'[[[[[1]]]],[[[[1]]]]]'})
I think it would be easier to just loop and use join. Your test cases pass.
function out = bracketarray_matlabbit(in)
out = permute(in, [2 1 3:ndims(in)]);
out = string(out);
dimsToCat = ndims(out);
if iscolumn(out)
dimsToCat = dimsToCat-1;
end
for i = 1:dimsToCat
out = "[" + join(out, ",", i) + "]";
end
end
This also seems to be faster than the route you were pursing:
>> x = permute(reshape([1:16],2,2,2,2),[2,1,3,4]);
>> tic; for i = 1:1e4; bracketarray_matlabbit(x); end; toc
Elapsed time is 0.187955 seconds.
>> tic; for i = 1:1e4; bracketarray_cris_luengo(x); end; toc
Elapsed time is 5.859952 seconds.
The recursive function is almost complete. What is missing is a way to index the last dimension. There are several ways to do this, the neatest, I find, is as follows:
n = ndims(x);
index = cell(n-1, 1);
index(:) = {':'};
y = x(index{:}, ii);
It's a little tricky at first, but this is what happens: index is a set of n-1 strings ':'. index{:} is a comma-separated list of these strings. When we index x(index{:},ii) we actually do x(:,:,:,ii) (if n is 4).
The completed recursive function is:
function out = bracketarray(in)
n = ndims(in);
if n == 2
% Fill in your n==2 code here
else
% if array has more than 2 dimensions, recursively send planes of 2 dimensions for encoding
index = cell(n-1, 1);
index(:) = {':'};
storage = cell(size(in, n), 1);
for ii = 1:size(in, n)
storage(ii) = bracketarray(in(index{:}, ii)); % last dimension automatically removed
end
end
out = { strcat('[', strjoin(storage, ','), ']') };
Note that I have preallocated the storage cell array, to prevent it from being resized in every loop iteration. You should do the same in your 2D case code. Preallocating is important in MATLAB for performance reasons, and the MATLAB Editor should warm you about this too.

Excel VBA: Mysterious Zero is Being Added to Array Created with "For" Loop

Hey everyone: I'm trying to make a function that digs through an array and adds values from range2 when the corresponding value from range1 equals criteria1.
I'm relatively new to VBA, so it's not the most elegant function in the world, but here's my code:
Function SingleArray(range1 As Range, range2 As Range, criteria1 As String)
Dim newrange() As Double
Dim d As Integer
Dim g As Integer
Dim i As Integer
g = Application.WorksheetFunction.CountIf(range1, criteria1)
ReDim newrange(g)
d = 1
For i = 0 To (range1.Count)
If range1(i) = criteria1 Then
newrange(d) = range2.Item(i).Value
d = d + 1
End If
Next i
SingleArray = newrange
End Function
Here is my data sample:
range2 range1
-5000 Bob
-5000 Jim
200 Bob
500 Jim
5000 Bob
200 Bob
300 Bob
1000 Bob
When I set the criteria as "Bob," the array that is returned is as follows:
{0,-5000,200,5000,200,300,1000}
I'm genuinely at a loss for how that zero is making it in there. Any thoughts you can provide would be most welcome!
1-D arrays default to a zero-based index structure (e.g. 0, 1, 2, 3, ....). You are looping through the ranges with a one based index (e.g. 1, 2, 3, 4, ...).
When you declare ReDim newrange(5) you are actually creating an array with six elements, not five (e.g. 0, 1, 2, 3, 4, 5)
You can make all arrays on that code sheet default to a one based index by putting this compiler directive at the top of the code sheet.
Option Base 1
You can also change the way the array is declared on the fly by specifying the Lower Boundary and the Upper Boundary.
ReDim newrange(1 to g)

Calculate fixed sum of array if some values changed

Say we are given a vector which sums to 1. Assume if values are higher than n, we maximize these to value n, and we want to make the rest of the numbers such that the sum equals 1 again. I.e. the other values need to (potentially) increase.
The other values must increase evenly (by the same factor)
Is there an easy way in excel to do this?
Ex. let n = 0.25
0.077331613 0.077331613
0.237037801 0.237037801
0.341441747 0.25
0.336289699 0.25
0.007899139 0.007899139
The second vector does not sum to 1, and we need to make sure it does without changing elements of value 0.25.
Not sure I agree that the required formulas are "long" and "complicated", nor "calculation intensive" :-).
Assuming your original values are in A2:A6, and your choice of n, e.g. 0.25, in C1, then, in B2:
=IF(A2>=C$1,C$1,A2*(1-C$1*COUNTIF(A$2:A$6,">="&C$1))/SUMIF(A$2:A$6,"<"&C$1))
Copy down to B6.
Regards
I was working on this UDF while the other answer was accepted so I'll post it here for future viewers of this thread.
Tap Alt+F11 then Alt+I, M and paste the following into a module code sheet. Tap Alt+Q to return to your worksheet.
Function udf_vector_elements(rng As Range, elm As Long, n As Double, ttl As Double)
Dim dTTL As Double
Dim v As Long, vELMs As Variant
Dim awf As WorksheetFunction
Dim iSAFETY As Long, iLESS As Long, dFUDGE As Double
Set awf = Application.WorksheetFunction
ReDim vELMs(1 To rng.Cells.Count)
'safety for not enough elements × n < ttl
If rng.Cells.Count * n < ttl Then
udf_vector_elements = "never enough"
Exit Function
End If
'seed the solution array
For v = LBound(vELMs) To UBound(vELMs)
If rng.Cells(v).Value2 < n Then
vELMs(v) = rng.Cells(v).Value2
iLESS = iLESS + 1
Else
vELMs(v) = n
End If
Next v
'safety for sum(elements) > ttl
If awf.Sum(vELMs) > ttl Then
udf_vector_elements = "already too much"
Exit Function
End If
'get to work
Do While Round(awf.Sum(vELMs), 6) < ttl
dFUDGE = (ttl - Round(awf.Sum(vELMs), 6)) / iLESS
iLESS = 0
For v = LBound(vELMs) To UBound(vELMs)
vELMs(v) = Round(awf.Min(vELMs(v) + dFUDGE, n), 6)
iLESS = iLESS - (vELMs(v) <> n)
Next v
Loop
'debug.print
dTTL = awf.Sum(vELMs)
Set awf = Nothing
'return the individual adjusted vector array element
udf_vector_elements = vELMs(elm)
End Function
Use as any other native worksheet function.
=udf_vector_elements(<vector_values>, <element_returned>, <max_n>, <target_total>)
In the below image in C2 as,
=udf_vector_elements(A$2:A$6, ROW(1:1), E$1, 1)
        
Fill down as necessary.

Return matrices of row and column indices

I am sure this question must be answered somewhere else but I can't seem to find the answer.
Given a matrix M, what is the most efficient/succinct way to return two matrices respectively containing the row and column indices of the elements of M.
E.g.
M = [1 5 ; NaN 2]
and I want
MRow = [1 1; 2 2]
MCol = [1 2; 1 2]
One way would be to do
[MRow, MCol] = find(ones(size(M)))
MRow = reshape(MRow, size(M))
MCol = reshape(MCol, size(M))
But this does not seem particular succinct nor efficient.
This essentially amounts to building a regular grid over possible values of row and column indices. It can be achieved using meshgrid, which is more effective than using find as it avoids building the matrix of ones and trying to "find" a result that is essentially already known.
M = [1 5 ; NaN 2];
[nRows, nCols] = size(M);
[MCol, MRow] = meshgrid(1:nCols, 1:nRows);
Use meshgrid:
[mcol, mrow] = meshgrid(1:size(M,2),1:size(M,1))

Resources