while reading someone's code I came across this:
data = [data.a, data.b, data.c, ...
data.x, data.y'];
why does the y have a single quote after it? does it have something to do with its data type? I got this error after removing it:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
can someone please explain? thanks!
The single quotation mark is Matlab's transposition operator. If x is a row vector then x' is a column vector, and so forth.
If data.x and data.y are both row vectors, it's no surprise that your attempt to horizontally concatenate data.x and data.y' is unsuccessful, it's not a well-defined operation, since the former is (I guess) a row vector and the latter (if I guess correctly) a column vector.
Related
I have a row cell vector M, containing matrices in each cell. Every matrix m (matrix inside the big matrix M) is made of 2 channels (columns), of which I only want to use the first.
The approach I thought about was going through each m, check if it has 2 channels, and if that is the case delete the second channel.
Is there a way to just slice it in matlab? or loop it and obtain the matrix M as the matrix m would disappear.
First code is:
load('ECGdata.mat')
I have the below.
when I double-click in one of the variable , here is what I can see:
As you can see the length of each matrix in each cell is different. Now let's see one cell:
The loop I'm trying to get must check the shape of the matrix (I'm talking python here/ I mean if the matrix has 2 columns then delete the second) because some of the variables of the dataframe have matrix containing one column (or just a normal column).
In here I'm only showing the SR variable that has 2 columns for each matrix. Its not the case for the rest of the variables
You do not need to delete the extra "channel", what you can do is quite simple:
newVar = cellfun(#(x)x(:,1), varName, 'UniformOutput', false);
where varName is SR, VF etc. (just run this command once for each of the variables you load).
What the code above does is go over each element of the input cell (an Nx2 matrix in your example), and select the first column only. Then it stores all outputs in a new cell array. In case of matrices with a single column, there is no effect - we just get the input back.
(I apologize in advance if there is some typo / error in the code, as I am writing this answer from my phone and cannot test it. Please leave a comment if something is wrong, and I'll do my best to fix it tomorrow.)
New to Matlab and using the an e-book to learn.
This was the question:
Write a MATLAB program which takes as its input a character string and print out its
penultimate character.
The code I worked out:
A=char('X');
X=input('Please enter a string of characters: ','s');
disp(X(Size-1));
What am I doing wrong?
When I run it, the input part happens and then I get an error, I assume this is due to incorrect index reference?
Thanks
Size is a function, not a variable. Therefore, it would need arguments, like size(X) which would return [1 5] for a 1x5 matrix.
Try X(length(X)-1) or more concisely, X(end-1)
More info:
size(matrix) returns the dimensions of a matrix.
size(matrix, dimension) returns the number of elements in a dimension (row, column, etc) of a matrix.
numel(matrix) returns the number of elements of a matrix. For a 2D matrix, this is #rows * #cols. For any matrix, this is equivalent to prod(size(matrix)).
length(matrix) finds the number of elements along the largest dimension of a matrix. It is equivalent to max(size(matrix))
I got the following error while working with a MATLAB program:
Error using - Matrix dimensions must agree
I noticed that the sizes of the matrices I'm trying to subtract from each other were:
firstMatrix --> 425x356
secondMatrix --> 426x356
How can I make them of equal size and go ahead and do my subtraction process?
I tried reshape, but the number of elements here seem to have to be equal.
Thanks.
I think both answers are missing the key point. Blithely subtracting two arrays of different size forgets that those arrays are NOT just numbers. The numbers must mean something. Else, they are just meaningless.
As well, simply deleting a row from the beginning or end may well be wrong, or padding with zeros. Only you know what the numbers mean, and why those arrays are not the same size. So only you can decide what is the proper action.
It might be right to pad, delete, interpolate, do any of these things. Or you might realize there is a bug in your code that created these arrays.
Your matrices have a different number of elements, so there's no point using reshape here (since it maintains the total number of elements). You'll have to discard one of the lines in the larger matrix before doing the subtraction:
For instance, you can discard the last line:
firstMatrix - secondMatrix(1:end - 1, :)
or discard the first line:
firstMatrix - secondMatrix(2:end, :)
Alternatively, you can pad the smaller matrix with default values (e.g NaN or zeroes), as suggested in another answer.
You're missing a row in firstMatrix
So can try:
firstMatrix=[firstMatrix;zeros(1,356)];
This will add a row of zeros at end of firstMatrix making it of 426x356
The other day I discovered the following bug in a couple of places in my MATLAB code
I wanted to enter the column vector in my MATLAB script
[a-b,
c-d
e-f]
where a,b,c,d,e,f are long expressions in some variables.
and I entered it in as
[ a -b ;
c -d ;
e -f]
Now MATLAB interprets the second matrix as a 3x2 matrix instead of a column vector.
Is there a way/command/function to force MATLAB to use only the comma and NOT any white space characters as a column separator for matrices ?
I don't think there is any way to force matlab to not treat white space this way, since it is interpretive language, and doing so may affect some built-in functions/third-party code.
However, you can use parentheses to group data - i.e. (a -b) will still be a single element of the matrix.
Well your second matrix does look like it's intended as a 3x2. However, if you do it like this it will be a column vector again:
[a - b;
c - d;
e - f]
which to me is a reasonable intuitive distinction between a minus b and a, negative b.
You can also use brackets as Ilya suggested.
Assuming you have a piece of code in which you only want to have column vectors and no matrix, there is a fairly quick solution:
replace {space}+ by +
replace {space}- by -
It is quite safe to do and unless you have complicated expressions in your vector it should do the trick.
I am getting stock prices from yahoo, and want to have each stock have its own time series data structure, but also don't want to have hundreds of variables, so naturally I would want to have an array, but when I do something like array = [stock1 stock2]; it actually merges the series together. How can I make a real array?
Thanks,
CP
[x x] notation in matlab is not an array, it is a vector. It is assumed that what you're putting together belongs together. What you probably want is a cell array which is indexed with a curly brace, ie myArray{1} = stock1; myArray{2} = stock2;. Reference here.
Ah, since you have row vectors, [stock1 stock2] is a concatenation. If you want to create a 2-by-x array instead, do something like this [stock1; stock2], which will place one array above the other.
Joining vectors using [x y] has different results depending on whether your vectors are rows or columns. If rows, then joining them with [x y] makes a longer row vector, but if columns, you'll get a Nx2 matrix. You should probably convert them to column vectors using the TRANSPOSE operator thus: [x' y']. Although you should check if transpose means the same thing with Time Series objects as at does with regular vectors.