I have a question about being able to bring some number to the power of an element in a matrix.
I know that, if A is a matrix, then one could write A.^2 to take the square of each number in that matrix. My question is, is there any way to something like: B=2.^A, such that the resulting matrix B is the same size as A, and each element in that matrix is equal to 2 to the power of the corresponding element in A?
Thanks for any help!
You already answered yourself! Use B = 2.^A.
For example:
>> A = [1 2; 3 4]
A =
1 2
3 4
>> B = 2.^A
B =
2 4
8 16
You could also use power(2,A), which is the same thing.
Matlab is a very interactive platform, so feel free to experiment and see for yourself if something works or not. In this case your intuition was correct.
Related
I am trying to add two vectors; however, they do not have the same dimensions. For example only (since the one I am doing have 1000+ values), the vectors are:
a = [1 2 3 4 5];
b = [1 2];
Since they do not have the same dimensions, I want to simply add zeroes to vector b to match the dimension of vector a.
Using the code b(numel(a)) = 0; I was able to do it. However, I am quite confused on how it worked as I only saw this code on the internet. I know that numel(a) is equal to 5, but I don't know how that code was able to add zeroes after 1 2 in variable b to match the dimension of variable a.
Can anyone explain?
When you set b(5) = 0, matlab can not simply leave the intervening elements b(3:4) unfilled, so they get zeros. If you did b(numel(a)) = 1, the intervening elements would still be filled with zero.
Keep in mind that this is a short cut that only works if you know for that fact that b is shorter than a. If not, you will be setting an element of b to 0, which is likely not what you want.
I'm sure this is a trivial question for a signals person. I need to find the function in Matlab that outputs averaging of contiguous segments of windowsize= l of a vector, e.g.
origSignal: [1 2 3 4 5 6 7 8 9];
windowSize = 3;
output = [2 5 8]; % i.e. [(1+2+3)/3 (4+5+6)/3 (7+8+9)/3]
EDIT: Neither one of the options presented in How can I (efficiently) compute a moving average of a vector? seems to work because I need that the window of size 3 slides, and doesnt include any of the previous elements... Maybe I'm missing it. Take a look at my example...
Thanks!
If the size of the original data is always a multiple of widowsize:
mean(reshape(origSignal,windowSize,[]));
Else, in one line:
mean(reshape(origSignal(1:end-mod(length(origSignal),windowSize)),windowSize,[]))
This is the same as before, but the signal is only taken to the end minus the extra values less than windowsize.
I'm still fairly new to working with Matlab and programming. I have a dataset with n trials, of these (in this case) m are relevant. So I have an m-by-1 vector with the indices of the relevant trials (rel). I have another vector (Correct which is n-by-1) that consists of 0 and 1. n is always bigger than m. I need to know which trials (of the m-by-1 relevant trials) have a 1 in the n-by-1 vector. I have tried for-loops but I always get an error 'Index exceeds matrix dimensions.'
Here is my code:
for i=1:length(rel);
CC=rel(find(Correct==1));
end;
I think it should be fairly simple but I don't know yet how to explain to Matlab what I want...
Thank you all for your answers. I realized that my question was not as clear as I thought (also a learning process I guess..) so your suggestions weren't exactly what I need. I'm sorry for being unclear.
Correct is not a logical, it does contain 0 and 1 but these refer to correct or incorrect answer (I'm actually not sure if this matters but I thought I let you know)
rel is a subset of the original data with all trials (all trials=n trials), Correct is the same length as the original vector with all trials (n trials). So rel contains the indices of the (for me) relevant trials of the original data and is that way connected to Correct.
I hope this makes my question a bit more clear, if not, let me know!
Thank you!
It's not quite clear from your question what you are trying to do but I think I have an idea.
You have a vector n similar to
>> n = round(rand(1, 10))
n =
0 1 1 0 0 0 1 0 0 1
and m is indices of this vector similar to
>> m = [1 3 7 9];
Now we use m to index n as n(m) which will return the values of n corresponding to the elements in m. Next we need to check these for equality with 1 as n(m) == 1 and finally we need to figure out what values of m have n equal to 1 again by indexing. So putting this altogether we get
>> m(n(m) == 1)
ans =
3 7
To find the indices of m that are being returned you can use
>> find(n(m) == 1)
ans =
2 4
I'm assuming that Correct is of type logical (i.e. it contains trues and falses instead of 0s and 1s).
You actually don't need a loop here (this is clear in your case, since you are looping over i and never actually use i in your loop):
m = numel(rel)
CC = rel(Correct(1:m))
The reason you are getting that error is because Correct has more elements than rel so you are attempting to address elements beyond the end of rel. I solve this above by only considering the first m elements of Correct.
H = [1 2; 3 4; 5 6; 7 8; 9 10; 11 12; 13 14; 15 16];
X = [7; 9];
Y = H*X;
H1 = Y/X;
This is my code. As you can see, I was trying to get back the H values. However, it gave me something else. I have tried to use inv() but this is not possible because X is not a square matrix.
You can't get a value of rank 2 back by dividing a value of rank 1. The system is underconstrained.
Both mrdivide and pinv (for pseudo-inverse) can be used to get a solution to the system. Because there are multiple solutions, it wouldn't necessary be the one you started with. Instead you'll get a "simplest" solution, either in the sense of lowest cardinality or lowest 2-norm, depending on whether you use mrdivide or pinv.
Here, the pinv documentation page probably explains it more precisely than I can. Just note it is discussing X\Y instead of Y/X:
If A has more rows than columns and is not of full rank, then the overdetermined least squares problem
minimize norm(A*x-b)
does not have a unique solution. Two of the infinitely many solutions are
x = pinv(A)*b
and
y = A\b
These two are distinguished by the facts that norm(x) is smaller than the norm of any other solution and that y has the fewest possible nonzero components.
I have this little problem and I hope that you can help me.
My question is about if there is someway to make this operation in MATLAB:
Suppose this matrix called A(4x3):
A=[1 2 3;4 5 6;7 8 9;8 9 1];
and this vector array called B (4x1):
B=[1;3;5;0];
Now the operation that I want to make is kinda simple: A+B=C, where C is:
A + B = C
C=[2 3 4;7 8 9;12 13 14;8 9 1];
As you can see, the first row of the matrix C is the sum between the first row of matrix A with the first value of the vector B, and it continues.
I know how to make it easily using a "for" but I want to know if there's a way to make it faster.
bsxfun [Apply element-by-element binary operation to two arrays with singleton expansion enabled] with a function handle #plus might just work for you. It lets B expand onto the second dimension as needed for operating with A which is already a 2D matrix and thus giving you the desired "summation" output -
bsxfun(#plus,A,B)