I have a Matlab code to generate a title representing the multiplication of two fractions using Latex.
t=title('$\frac{5}{2} \times \frac{3}{4}$');
set(t,'Interpreter','Latex');
My question is how to replace the numbers in Latex equation to variables?
For example, if I defined
A = 5;
B = 2;
C = 3;
D = 4;
How to use A,B,C,D to replace the numbers on the latex form title?
You will need to either use [] to concatenate the string
titlestr = ['$\frac{', num2str(A), '}{', num2str(B), '} \times \frac{', num2str(C), '}{', num2str(D)'}$'];
title(titlestr)
Or you can use sprintf but you'll have to be sure to escape all of the \ characters
titlestr = sprintf('$\\frac{%d}{%d} \\times \\frac{%d}{%d}$', A, B, C, D);
title(titlestr)
Related
How do i print a vector in matlab with also something before the values. For example i have a vector with the following values A' = [1 2 3]. I want to print it out in such a way that the output will be
w0 = 1
w1 = 2
w2 = 3
How do i do this?
Can i do this using a loop?
I am using the following code and i am not getting the right output
for qux = 1:3
fprintf('w%i=%.4lf\n',qux-1,answ);
end
Output:
w0=w1=w2
Your format string is not formed properly. Specifically '%.4lf' should be '%.4f'. Additionally, the third input to fprintf should be A(qux) to access the value in A.
for qux = 1:3
fprintf('w%i=%.4f\n', qux-1, A(qux));
end
I would, however, recommend using '%g' to use a format that optimizes the display of each number. Additionally, you could remove the for loop and do something like
A = [1, 2, 3];
fprintf('w%i = %g\n', [0:numel(A)-1; A])
If I have understand the question correctly, I think that you could do that with the following code:
for i = 1:3
disp(['w' num2str(A(i)-1) '=' num2str(A(i))]);
end
Using disp and num2str you could get the following output:
w0=1
w1=2
w2=3
Problem description:
trs takes a row or column vector of string, and two characters as ch1 and ch2. If ch1 is matched with any element in trs then that element of trs will be replaced by ch2. If ch1 is not in the vector, trs will be returned as unchanged.
A simple example:
Input: ww = kellen({‘YOYO’, ‘YOYO’},’Y’,’X’),
output: ww = {‘XOXO’,’XOXO’}
I assume strrep function could make this problem easier but I would like to know the very basic level how MATLAB can handle this problem without using strrep function. So I request you guys please correct my code without using strrepfunction.
I am new to MATLAB. Indeed, I am new to programming too. I know I had to learn C first for basic but I did not that why is I am to struggle.
Here is my code but it seems do not work.
function ww = kellen(trs,ch1,ch2)
[r c] = size(trs);
if r == 1 && c > 1
for i = 1:c
ind = trs{i} == ch1;
trs{1,i}(ind==1) = ch2;
ww = trs;
end
if r == 1 && c ==1
for i = 1:c
ind = trs{i} == ch1;
trs{1,i}(ind==1) = ch2;
ww = trs
end
end
My code works fine when size of string vector is row vector but my function is not working fine when i pass scalar of trs string. For instance:
kellen({ 'peter piper picked a peck of pickled peppers' }, 'p', 'b')
Which part of my code should i modify?
for i = 1:c
if trs{i} == 'c1'
outp = [trs 'c2'];
else
return
end
end
The first problem I see with your code is the line: if trs{i} == 'c1'
There are a lot of problems here:
By putting '...' quotes around ch1 you are making ch1 a string literal, NOT the variable for character one that was passed into your function. Drop the quotes. This is the source of your error, attempting to equate a 4 character string with a 3 character string.
You are equating a whole string trs{i} with a single character ch1. You can do that in Matlab, but NOT in an if statement. Let's take your example inputs, trs{1} is 'YOYO' and if we try 'YOYO'=='Y' we get a logical vector like [1 0 1 0]. Now if expects just a 1 or 0 and not a vector.
So I would suggest dropping the if statement and taking advantage of Matlab's logical indexing instead:
outp{c} = []; %// this is just preallocation
for i = 1:c
temp = trs{i};
idx = temp==ch1 %// Get a logical matrix of which characters match
temp(idx)=ch2; %// Use logical indexing to replace all the characters that match in this string in one go!
outp{i} = temp;
end
Once you understand this code you can simplify:
outp = trs;
for i = 1:c
outp{i}(outp{i}==ch1)=ch2
end
You can use ismember() to find the locations where ch1 is present in trs. So, in fact trs(ismember(trs,ch1)) = ch2; will replace all instances of ch1 with ch2 in trs.
However, you need to make sure that ch1 and ch2 are exactly one characters long. If you want to have trs as a cell of strings, like in your question, than you can either loop through it the way you do now, or you can take a look at the cellfun() function.
Example: I have an array like this R = sym('R',[4 4]). I do some symbolic operations and get an expression, which is a function of stuff like R1_2, R2_2, etc. I'd like to paste the expression into some code, but I really want it to look like R(1,2), R(2,2) etc. Is there a function to do this, or do I need to manually find/replace 16 times?
You can substitute your variable R with a unknown function R:
R = sym('R',[3 3]);
M=det(R)
funR = symfun(sym('R(x, y)'),[sym('x'),sym('y')]);
for rndx=1:size(R,1)
for cndx=1:size(R,2)
M=subs(M,R(rndx,cndx),funR(rndx,cndx));
end
end
Output:
R(1, 1)*R(2, 2)*R(3, 3) - R(1, 1)*R(2, 3)*R(3, 2) - R(1, 2)*R(2, 1)*R(3, 3) + R(1, 2)*R(3, 1)*R(2, 3) + R(2, 1)*R(1, 3)*R(3, 2) - R(1, 3)*R(2, 2)*R(3, 1)
vectorized version of the code above (faster):
[rndx,cndx]=ind2sub(size(R),1:numel(R));
M2=subs(M,num2cell(R(:))',num2cell(funR(rndx,cndx)))
You can use regular expressions for that.
As an example I'm using the determinant function, and I'm defining R of size 3x3 to save space. But the code is generic.
R = sym('R',[3 3]); %// example matrix
f = det(R); %// example function
str = char(f); %// convert to string
[split, match] = regexp(str, '\d+_\d+','split','match'); %// split string according
%// to pattern "digits underscore digits"
match2 = cellfun(# (x) ['(' regexprep(x, '_', ',') ')'] , match, 'uniformoutput', 0);
%// replace `_` by `,` and include parentheses
match2{end+1} = ''; %// equalize number of cells, for concatenation
result = [split; match2]; %// concatenate cells
result = [result{:}]; %// concatenage strings
In this example, the symbolic function f
f =
R1_1*R2_2*R3_3 - R1_1*R2_3*R3_2 - R1_2*R2_1*R3_3 + R1_2*R2_3*R3_1 + R1_3*R2_1*R3_2 - R1_3*R2_2*R3_1
gives the following string as result:
result =
R(1,1)*R(2,2)*R(3,3) - R(1,1)*R(2,3)*R(3,2) - R(1,2)*R(2,1)*R(3,3) + R(1,2)*R(2,3)*R(3,1) + R(1,3)*R(2,1)*R(3,2) - R(1,3)*R(2,2)*R(3,1)
If I have an input like [1 2 3; 4.0 c] and I want it to output it like 1234.0c in matlab. What function can I use ? I am looking for something like trim in php.
Any idea ?
Thanks
You can use this to remove any number of spaces from inside of a string:
>> a = char(' he llo wor ld ');
>> a(isspace(a)) = [] %replaces all of the space with nothing
a =
helloworld
You can use isstrprop function with approppriate categories. For your case,
>> str = '1 2 3; 4.0 c';
>> str(isstrprop(str, 'alphanum') | str == '.')
ans =
1234.0c
You can use functions like isletter, isnumeric, etc. if you like.
Besides, you can create your own function in one line as follows
>> myTrim = #(x)(x(isstrprop(x, 'alphanum') | x == '.'));
>> myTrim(str)
ans =
1234.0c
Note that you ask [1 2 3; 4.0 c] as an input which is not a proper syntax for MATLAB. I assumed you wanted to ask for a string. In addition, trim actually implies removing leading and trailing white space from a string and there is strtrim for this in MATLAB.
It is not a valid MATLAB argument.
But if you have something like
a = ['1', '2' ,'3'; '4', '.','c'];
you can use
a(:)'
to get
142.3c
or
a = a';
a(:)'
to get
123.4c
How would I generate a vector like
x1,x2,x3,x4,...,xn
the problem is concatenate ','and a 'x' char
n=100
A = (1:n);
This is a slight improvement on #Jonas's answer. SPRINTF will do the repeating for you avoiding the need for a mask:
>> n = 5;
>> out = sprintf('x%u,', 1:n);
>> out(end) = []
out =
x1,x2,x3,x4,x5
To generate the string 'x1,x2' etc, you can create a mask for SPRINTF using REPMAT like so:
n = 5;
mask = repmat('x%i,',1,n);
out = sprintf(mask,1:n);
out = out(1:end-1)
out =
x1,x2,x3,x4,x5
Note that in case you actually want to create a vector containing the strings 'x1','x2' etc, you'd use ARRAYFUN to generate a cell array:
out = arrayfun(#(x)sprintf('x%i',x),1:n,'uniformOutput',false)
out =
'x1' 'x2' 'x3' 'x4' 'x5'
The better answer is, don't do it. While you CAN do so, this will likely cause more heartache for you in the future than you want. Having hundreds of such variables floating around is silly, when you can use an array to index the same data. Thus perhaps x{1}, x{2}, ....