How to evaluate a matlab symbolic expression properly? [duplicate] - matlab

This question already has an answer here:
Evaluate symbolic expression
(1 answer)
Closed 4 years ago.
Lets say I have a matrix like this:
syms p;
K = [p^2+3 0; 2 5*p];
p_initial = 2;
Whats the proper/fastest way of getting K(p_initial), that is the resulting matrix if I insert 2 for p. Further, I want the resulting matrix to be of type double, not of symbolic type.
Thanks in advance

Use subs to substitute variables in symbolic expressions
subs(K,'p',p_initial)
ans =
[ 7, 0]
[ 2, 10]

Related

Finding value that is similar and available in another vector [duplicate]

This question already has an answer here:
Matlab, finding common values in two array
(1 answer)
Closed 5 years ago.
Let say we have 2 vectors of A and B,
A=[1;2;5;6;7;9]; B=[1;3;4;7];
How to find value C that are available in both A and B? The expected value should be
C=[1;7]
Since the title of your question says "similar", I assume you want to compare with a given tolerance. For that you can use ismembertol:
tol = 1e-3;
A = [1; 2 ; 5 ; 6 ; 7 ; 9];
B = [1.0001; 3.0001; 4.0001; 7.0001];
ind = ismembertol(A, B, tol);
C = A(ind);
Very simple:
A=[1;2;5;6;7;9];
B=[1;3;4;7];
C=intersect(A,B)

Error about Subscript indices of sum function for float matrix matlab [duplicate]

This question already has an answer here:
matlab strange behaviour of function sum()
(1 answer)
Closed 7 years ago.
I have a matrix 202x141x3 that called M matrix. I want to perform the code
sum(M,3);
However, I got error such as
Subscript indices must either be real positive integers or logicals.
I debugged and saw the content in M matrix, it has not problem. But I cannot use the above function. Could you see my M.mat matrix at here and let me know how can I use above function for my M.mat matrix
You did set a variable under the name sum
a = [1 2 3 ; 4 -5 6; 7 8 9]
sum = 1;
>> sum(a)
Subscript indices must either be real positive integers or logicals.
clear sum;
>> sum(a)
ans =
12 5 18

What does the tilde symbol after an equals do? [duplicate]

This question already has an answer here:
Matlab Operators
(1 answer)
Closed 9 years ago.
What does the tilde symbol ~ do in Matlab?
for example, I've got the Matlab line, in which a and b are tables.
a =~b;
Basically a is assigned to the result of the logical not operator applied to b
For example if b is the matrix
b = [12 0 10]
then
a = ~b
a = [0 1 0]
See http://www.mathworks.co.uk/help/matlab/ref/not.html for details

transcedental equations in MATLAB [duplicate]

This question already has answers here:
solving nonlinear equations in Octave
(2 answers)
Closed 9 years ago.
How to solve a equation like 3^x + 4^x = 6^x in MATLAB . I want the solution exact to eight decimal digits .
I tried a very simplistic way but there is not enough memory for that . Since I know the solution is between 1 and 2 , I thought of creating an array x = [1:10^-9:2] and then use these arrays to find the value of correct x . I know this is very naive method .
How does one go about solving such equations in MATLAB ?
Use fzero:
>> f = #(x) 3^x + 4^x - 6^x
f =
#(x)3^x+4^x-6^x
>> x0 = [1 2]
x0 =
1 2
>> format long g
>> fzero(f,x0)
ans =
1.293174075673

How to convert array of bits to integer in Matlab? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Converting binary to decimal without using loop
I'm looking for the simplest and fastest solution. I tried documentation but could find anything.
I have an array of bits like [0, 1, 1, 1] and I want to convert it to simple int number 7. I get this array with bitget(x, 1:3), where x is an integer.
Just as a pedagogical alternative to #Edwin and #Azim's solution's (which are better for production), try
b = [1 0 0 1]; % or whatever
sum(b.*(2.^[length(b)-1 : -1 : 0])) % => 9 for the above
We create the basis elements with 2.^[length(b)-1 : -1 : 0] = [8 4 2 1], multiply each element by the binary value to get [8 0 0 1], then sum to get the final answer.
#Edwin's answer uses binvec2dec which is part of the Data Acquisition Toolbox. This toobox is an additional toolbox (developed by Mathworks) but not part of the base MATLAB package.
Here is a solution that does not depend on this toolbox.
Use num2str to convert the binary array to a string
str=num2str(bin_vec);
use bin2dec to get decimal value
dec_num=bin2dec(str);
A little rusty on Matlab, but this should work.
% This assumes you're using a vector of dimension 1 x n (i.e. horizontal vector)
% Otherwise, use flipud instead of fliplr
function [dec_num] = convert(bin_vec)
bin_vec = fliplr(bin_vec);
dec_num = binvec2dec(bin_vec);
% EDIT: This should still work
num = convert(bitget(x, 1:3);
For future reference, if this is about homework, use the homework tag.
binvec2dec Documentation
fliplr Documentation
flipud Documentation