Why won't the following run?
if (map [ [a b c] -> a + b = c ] [1] [2] [3]) [show 100]
The following produces 'true' as an output:
show (map [ [a b c] -> a + b = c ] [1] [2] [3])
so I expected the first statement above to be the same as:
if true [show 100]
(P.S. in my full version the lists are longer but are collapsed into a single true/false using reduce.)
Thanks.
To elaborate on ThomasC's comment, map always produces a list, even if it only has one element. So
(map [ [a b c] -> a + b = c ] [1] [2] [3])
does produce [true]. Thus
(map [ [a b c] -> a + b = c ] [1 2] [2 3] [3 5])
will produce [true true]. reduce is helpful here,
reduce AND (map [ [a b c] -> a + b = c ] [1 2] [2 3] [3 5])
will produce a simple true by "anding" all the elements of the map output, and
reduce AND (map [ [a b c] -> a + b = c ] [1 2] [2 3] [3 6])
will produce a simple false.
Related
I'm trying to use LU method to solve Ax=B, and when I do start doing so, using:
(P, L, U := LUDecomposition(A))
to create my three different matrices (P, L, U) I get the error
Error, cannot split rhs for multiple assignment
which doesn't make sense since LUDecomposition creates 3 matrices, which correspond to my P, L, U.
If you do either of the following then the call will return an expression sequence of containing three Matrices, and it will work for input A a Matrix.
with(LinearAlgebra):
P, L, U := LUDecomposition(A);
Or,
P, L, U := LinearAlgebra:-LUDecomposition(A);
But if you don't load the LinearAlgebra package and you also don't use the long-form name of that package's export then you're simply making a function call to an undefined name.
And, if you are in fact accidentally simply calling the undefined name LUDecomposition (because you have not used one of the mentioned methods for calling the package export of that name), then the result will be an unevaluated function call. And you cannot assign that single thing to three names on the left-hand-side of an assignment statement.
restart;
A := Matrix([[1,2],[3,5]]):
# Look, this next one does no computation.
# The return value is a single thing, an unevaluated
# function call.
LUDecomposition(A);
/[1 2]\
LUDecomposition|[ ]|
\[3 5]/
P, L, U := LUDecomposition(A);
Error, cannot split rhs for multiple assignment
But this would return the actual result,
LinearAlgebra:-LUDecomposition(A);
[1 0] [1 0] [1 2]
[ ], [ ], [ ]
[0 1] [3 1] [0 -1]
And so those three Matrices could be assigned to three names.
P, L, U := LinearAlgebra:-LUDecomposition(A);
[1 0] [1 0] [1 2]
P, L, U := [ ], [ ], [ ]
[0 1] [3 1] [0 -1]
Another way to do it is to first load the whole package, so that you can utilize the short-form command name,
with(LinearAlgebra):
LUDecomposition(A);
[1 0] [1 0] [1 2]
[ ], [ ], [ ]
[0 1] [3 1] [0 -1]
P, L, U := LUDecomposition(A);
[1 0] [1 0] [1 2]
P, L, U := [ ], [ ], [ ]
[0 1] [3 1] [0 -1]
I'm trying to append [1 2.2] to {'foo' 'ba'} and get:
'foo' 'ba'
1 2.200000
I'm nearly there:
>> A = {'foo' 'ba'}
A =
1×2 cell array
'foo' 'ba'
>> b = [1 2.2]
b =
1.000000000000000 2.200000000000000
>> [A;b]
Error using vertcat
Dimensions of matrices being concatenated are
not consistent.
>> [A;num2cell(b)]
ans =
2×2 cell array
'foo' 'ba'
[ 1] [2.200000000000000]
How to get rid of the []?
[A;num2cell(b)] is the correct implementation.
You can try doing
[A(1);b(1)]
ans =
'foo'
[ 1]
Notice the vector b element 1 represented as [ 1] which are equalivalent.
A = {'foo' 'ba'} ;
b = {'1' '2.2'} ;
[A ;b]
or
vertcat(A,b)
I have A, b, f matrices as follows :
A = [ 2 1
1 2 ]
B = [ 4; 3 ]
F = [ 1; 1 ]
LB = [ 0; 0 ]
[X, fval, exitflag, output, lambda] = linprog(F, A, B, [], [], LB)
After that, the solution provided by the MATLAB is surprising. It says the value of fval is 1.2169e-013. This is not the solution. Can you help me by identifying the fault?
I am looking a command that would do:
a*b*c -----> +
-a --------> -
a*b -------> +
c*d*e*f*a--> +
where a, b, c, d, e and f are symbolic variables in Matlab.
Is there any command to return the initial sign of an expression?
If you have Matlab 2013 you can do this:
>> syms a b c
>> children(a*b*c)
ans =
[ a, b, c]
>> children(-a)
ans =
[ a, -1]
>> children(a*b)
ans =
[ a, b]
>> children(-a*-b)
ans =
[ a, b]
>> children(-a*-b*-c)
ans =
[ a, b, c, -1]
You will get the initial sign by looking at the the last element of the returned vector.
So test for that.
If you define a variable to a value the result will look like this:
>> c = -4;
>> children(-a*-b*-c)
ans =
[ a, b, 4]
Calling sign on the last element will give -1 if negative, 1 for positive.
Note that there may not always be a numeric value as the last element!
Sign(a) will give sign(a) so you will need to assume it is positive in that case.
Say I have two arrays where one is a permutation of the other:
A = [2 1 5 3 7]
B = [7 2 1 3 5]
with no repetitions in either array.
How can I obtain the permutation mapping between both?
E.g. A->B should be:
[2, 3, 5, 4, 1]
which means:
A(1) -> B(2)
A(2) -> B(3)
A(3) -> B(5)
A(4) -> B(4)
A(5) -> B(1)
Update:
Is there a fast vectorized solution that does not use ismember ? In my experience, ismember tends to be slow for very large arrays.
How about this:
[i a] = sort(A);
[i b] = sort(B);
mapping = b(a)
Use ismember.
[~,idx] = ismember(A,B);
The vector idx will contain indices such that B(idx) == A.
Note that ismember finds the highest indices.
You can also use knnsearch, but for not repeated members in both a and b
Try this:
for i=1:size(B)
C(:,i) = [i find(B==A(i))]
end