Using octave I want to split a string into its individual characters.
How do I do this?
For example converting
x = "hello"
to
c = [h, e, l, l, o]
Thanks in advance for the help
It's already split in Matlab's string actually:
x = 'hello'
x(1)
>> h
Related
Why I can't plot the following code? The error is:
Index exceeds the number of array elements (1).
What I'm trying to achive is that a user enters a scalar value f (such as 1500) anf Ht and Hr. So, I want to find value of ahr and L accondingliy and then plot L vs f. SO on f axis I shoud have 100,150,200,250,300,....,1500 and corrsponding values on L axis.
for i = 100:50:f
cm = 0;
ahr(i) = (1.1*log10(f(i))-0.7)*Hr-1.56*log10(f(i))-0.8;
L(i) = 46.3+33.9*log10(f(i))-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d)+cm;
plot(f,L)
end
Using Vectorized Approach to Evaluate Plots
Assuming d, Ht and Hr are scalars the following code should work. This script uses a vectorized approach where f is a vector that is used to evaluate the equations ahr and L. The equations ahr and L will now be evaluated for every element in f resulting in the ahr and L being the same length as f. This method eliminates the need for a for-loop and works with MATLAB's strengths/capabilities.
%Asking user for input values%
fmax = input("Please type in the maximum f: ");
Ht = input("Please type in the value of Ht: ");
Hr = input("Please type in the value of Hr: ");
cm = 0;
d = 1;
%Creating the vector of points to plot on%
f = (100:50:fmax);
ahr = (1.1*log10(f)-0.7)*Hr-1.56*log10(f) - 0.8;
L = 46.3 + 33.9*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d);
clf;
plot(f,L,'Marker','.');
title("Plot L vs f");
xlabel("f"); ylabel("L");
xticks(f);
grid;
Ran using MATLAB 2019b
I would like to generate a string in Matlab that looks like
"BBBBBBBBBBBBBBBBCCCCCCCCCCCCCC"
where "B" is repeated m times and "C" is repeated n times. Is there any function with inputs similar to ("B","C",n,m) doing this?
You can use the function repelem to repeat the characters the wanted number of times.
str = 'BC' %This is character vector, NOT a string
n = 4; m = 3;
res = repelem(str,[n,m])
res =
'BBBBCCC'
repmat function works on char arrays, and concatenation operators too.
So:
copyfcn = #(B,C,n,m) [repmat(B,[1 n]) repmat(C,[1,m])];
copyfcn('B','C',8,4)
ans =
'BBBBBBBCCCC'
If you want the output to be a string rather than a char array, you can just wrap it in the string() function.
copyfcn2 = #(B,C,n,m) string([repmat(B,[1 n]) repmat(C,[1,m])]);
copyfcn2('B','C',8,4)
ans =
"BBBBBBBBCCCC"
You can make your own function using repmat:
>> f = #(a,b,n,m) [repmat(a, [1 m]) repmat(b, [1 m])];
>> f('B','C',12,14)
ans =
'BBBBBBBBBBBBBBCCCCCCCCCCCCCC'
I have two 1x6 vectors that I am eventually trying to just sum up, but I need to get all of the possible combinations of these vectors before doing so. The vectors will look like so:
V1=[a b c d e f];
V2=[A B C D E F];
What I need is to find all possible combinations of variables that will remain a 1x6 vector. I have been messing around for a while now and I think I have found a way by using various matrices but it seems terribly inefficient. An example of what I am looking for is as follows.
M=[a b c d e f;
A b c d e f;
A B c d e f;
A B C d e f;
A B C D e f;
A B C D E f;
A B C D E F;
. . .]
And so on and so forth until all combinations are found. Unfortunately I am not a MATLAB whiz hence the reason I'm reaching out. I'm sure there has to be a much simpler way than what I have been trying. I hope that my question was relatively clear. Any help is much appreciated! Thanks!
I used cellfun to create the indexes:
V1=['abcdef'];
V2=['ABCDEF'];
VV = [V1;V2];
l = length(V1);
pows = 0:l-1;
x = num2cell(2.^pows);
L = x{end};
rows = cellfun(#(x) reshape([ones(x,L/x);2*ones(x,L/x)],[2*L 1]),x,'Uniformoutput',0);
rows = cell2mat(rows);
cols = repmat(1:l,[2*L 1]);
idxs = sub2ind(size(VV),rows,cols);
M = VV(idxs);
and you get:
M =
abcdef
Abcdef
aBcdef
ABcdef
abCdef
AbCdef
aBCdef
ABCdef
abcDef
AbcDef
...
For those super experts out there, I was wondering if you see a quick way to convert the following "for" loop into a one-line vector calculation that is more efficient.
%Define:
%A size (n,1)
%B size (n,m)
%C size (n,1)
B = [2 200; 3 300; 4 400];
C = [1;2;1];
for j=1:n
A(j) = B( j, C(j) );
end
So to be clear, is there any alternative way to express A, as a function of B and C, without having to write a loop?
Yes, there is:
A = B(sub2ind([n,m], (1:n).', C));
It depends on functions A, B, and C, but this might work:
j = 1:n;
A = B(j, C(j));
How do I write an expression in Matlab code involving summation of a variable and then how do I minimize the expression?
ex. I need to minimize the following function
E= \sum_{i,j}[C_{ij}(r_{ij}) + C2_{ij}(r_{ij})^2]
I need to minimize the above expression for any values of r_{ij}s where i and j vary.
I could use the fmincon() in MATLAB but I am unable to write my expression suitably to give it as input to the fmincon().
Thanks.
Try this:
E = sum(sum( C.*r + C2.*r.^2 ));
where C, C2 and r are matrices of the same shape.
fmincon and other optimization functions do not require you to write everything as an expression, they can optimize for functions as well.
function E = criterion(r, C, C2)
e = C.*r + C2.*r.^2;
E = sum(e(:));
I'm not completely sure about the syntax required by fmincon, but I guess it's something like E = f(theta), where theta is a parameter vector you want adjusted such that E is minimal. Since I don't find your problem clearly described, I will assume your parameters are C and C2 (in the case that r are your parameters, the case is similar and simpler).
As fmincon uses a vector to store the coefficients, we need a function that takes such a vector and transforms it into the sizes required by the criterion function above.
function E = criterionRolledC(theta,r)
assert(numel(theta)==2*numel(r), 'The size of theta has to be twice the size of r');
[M N] = size(r);
C = theta(1:M*N);
C2 = theta(M*N+1:end);
C = reshape(C , M, N);
C2 = reshape(C2, M, N);
E = criterion(r,C,C2);
That way, you can make an anonymous function that easily conforms to the interface of the optimizer: #(theta)(criterionRolledC(theta,rValues)) will do when the variable rValues in your current workspace contains your r values.
In case you want the exact opposite, i.e. your parameters are r, it is simpler:
function E = criterionRolledR(theta,C,C2)
assert(numel(theta)==numel(C), 'The size of theta has to be the same size as C');
assert(all(size(C)==size(C2)), 'C and C2 need to have the same size');
[M N] = size(C);
r = reshape(theta, M, N);
E = criterion(r,C,C2);
And you can construct an anonymous function similarly to the other case.