I have program for a hash function:
H(a || b || g || y1 || y2)
where a,b,g,y1,and y2 are inputs. Can anybody tell me what to do with these inputs if I am using SHA-2 as my hash function? does this mean I have to either concatenate all the inputs, do the OR operation, or something else??
You can xor them, if these inputs are numbers, or concatenate them using sentinels, if they are strings.
Related
The answer to the question asked here Why is complex conjugate transpose the default in Matlab
says that for complex numbers we can use ' symbol to denote the transpose operation that is used for real numbers. Mathematically, the transpose operation that is done for real valued numbers is denoted by the symbol (.)^T . For the transpose of complex numbers the equivalent symbol is (.)^H. The way it is done is -- First we take the conjugate of the complex number and then take its transpose. This is the operation (.)^H.
I want to implement the operation (.)^{*T} = (.)^H for complex number. I have used the symbol apostrophe for this. Please correct me where wrong.
I wanted to confirm if my implementation of the concept is correct or not using Matlab. For example, for real valued vector A_r, I want to multiply it with its transpose multiply_r = A_r*A_r'
Replicating the same for complex valued vector A_c, this operation would become multiply_c = A_c * A_c'
A_r =[1,2,3]; %real valued vector
B_r = A_r'; %transpose of real valued vector
multiply_r =A_r*B_r;
A_c = [1 + sqrt(-1)*1, 2+sqrt(-1)*2, 3+sqrt(-1)*3]; %complex valued vector
B_c = A_c'; %transpose of complex valued vector
multiply_c = A_c*B_c;
Is this okay?
UPDATE : I am trying to take the normal transpose of this complex valued array, so that it is arranged in 3 rows and 1 column intead of 1 row and 3 columns. Using the operator .' I am getting weird values because the array is now increased in size !! What is the proper way?
h = [ -5.1053 + 3.6797i 1.3327 + 5.7339i 4.1302 -10.7521i].'
h =
-5.1053 + 3.6797i
1.3327 + 5.7339i
4.1302
0 -10.7521i
As you noted, Matlab has both matrix "transpose" ((.)^T) and "conjugate transpose" ((.)^H) defined.
For real-valued transpose, you have transpose, that can be expressed as an operator .' (note the '.' before the '):
aT = transpose(a);
isequal( aT, a.' ); % transpose() and .' are the same
For complex conjugate transpose you have ctranspose, that can be expressed as an operator ' (note there is no . before the '):
aH = ctranspose(a);
isequal( aH, a' ); % ctranspose and ' are the same
You can verify using conj:
isequal( a', conj(a).' );
I'm trying to write a function that can be a scalar and a vector. This function should handle scalars, column vector and row vector. My thought was that I try with a for loop but I'm not successful in my attempts.
function f=funk1(x);
for i =1:length(x)
f=exp(-3*x(i).^2)-log(x(i)+0.6)+1/(x(i)-6);
end
end
This only return the last value of the vector (in my function) but I want it to return every value from my vector.
For this specific function you can vectorize, that is, do all computations element-wise at once without a loop. You only need to add a dot before *, ^, / operators when applied between arrays (log and exp are element-wise already):
function f = funk1(x);
f = exp(-3*x.^2)-log(x+0.6)+1./(x-6);
end
You missed indexing of f. Do that;
function f=funk1(x);
for i =1:length(x)
f(i)=exp(-3*x(i).^2)-log(x(i)+0.6)+1/(x(i)-6);
end
end
Imagine you have a function in MATLAB of two variables, f(x,y). And it was written in a way that x can be a scalar or a 1D array, but y is strictly meant to be a scalar. For an array x the function returns the array of elements of the same length as x.
Next, you need to refactor this function to accept both x and y to be 1D arrays of equal length, the value of a function f([x1,x1],[y1,y2]) = [f(x1,y1), f(x2,y2)].
How would you do this in the most efficient way?
This is exactly suited for arrayfun:
f = #(x,y) y*x.^2; %an example where y should not be an array, x can
fnew = #(xv,yv) arrayfun(f,xv,yv);
This new function will return f(xv(k),yv(k)) for each k as long as the two arrays are of the same size.
Note that arrayfun is quite slow, so using a loop to the same effect or implementing the vectorization for the specific function f (possibly making use of bsxfun) might be faster.
The explicit looping alternative would be
function fv=fnew(xv,yv)
if numel(xv)~=numel(yv)
exit('fnew> xv and yv should have same length');
end
fv=zeros(size(xv));
for k=1:numel(xv)
fv(k) = f(xv(k),yv(k));
end
You can spare some runtime by skipping the size check, similar checks are the reason why arrayfun tends to be slower.
I have a (transition) function defined by a matrix say P=[0.75,0.25;0.25,0.75] and I have a vector say X=[1,2,1] then i would like to find P(1,2)*P(2,1). How is the easiest way to generalise this? I tried creating a function handle for P(i,j) and then X_temp=[X(1:end-1);X(2:end)], using the function of each column and finally using the product function, but it seems a lot more comprehensive than it has to be.
The X i want to use is 1000 dimensional and P is 3x3 and I would have to repeat it a lot of times so speed I think will matter.
You can use sub2ind to get your relevant P values:
Ps = P(sub2ind(size(P), X(1:end-1), X(2:end)))
Now just multiply them all together:
prod(Ps)
EDIT:
For function handles you had the right idea, just make sure that you function itself handles vectors. For example lets say your function f(i,j) = i + j, I'm going to assume it's actually f(x) = x(1) + x(2) but I want it to handle many xs at once sof(x) = x(:,1) + x(:,2):
f = #(x)(x(:,1) + x(:,2))
f([X(1:end-1)', X(2:end)'])
OR
f = #(ii, jj)(ii + jj)
f(X(1:end-1)', X(2:end)') %//You don't actually need the transposes here anymore
just note that you need to use element wise operators such as .*, ./ and .^ etc instead of *, /,^...
I am writing an easy IF condition. The function is to judge each row of the matrix to be certain vectors. The code is as follows:
if (compareM(i,:)==[1, 0])||(compareM(i,:) ==[2, 1])
match_1 = match_1 +1;
else
mismatch_1 = mismatch_1 +1;
end
The error said ''Operands to the || and && operators must be convertible to logical scalar values''.
compareM is a n by 2 matrix and I wonder if the error is made by || operation. Thanks in advance!
If you are comparing vectors, not scalar values, you have to use | operator. As a result you get logical vector of element-by-element pairwise comparison. To use it in IF statement you have to convert either each logical statement (then use ||) or the result of | the to scalar with ALL, ANY or other function.
If you want to compare to vectors for equality use ISEQUAL function as
if isequal(compareM(i,:)==[1, 0]) || isequal(compareM(i,:)==[2, 1])
compareM(i, :) evaluates to a 1x2 numeric array, so compareM(i,:)==[1, 0] evaluates to a 1x2 logical array. The same for the expression to the right of the || sign. But you need a single logical value on each side of ||, not a 1x2 array of logical values.
If you want this expression to evaluate to true if both values on the lhs of == are the same as the corresponding elements on the rhs, wrap all() around each side:
all(compareM(i,:)==[1, 0]) || all(compareM(i,:) ==[2, 1])
if ((compareM(i,:)==[1, 0])||(compareM(i,:) ==[2, 1]))
match_1 = match_1 +1;
else
mismatch_1 = mismatch_1 +1;
end
Note the outer enclosing parentheses.