Cannot create symbolic variable n row 1 column in MATLAB - matlab

In MATLAB, I want to create symbolic variable n row 1 column as: [y1;y2;...;yn].
Then I use the code:
y=sym('y',[n,1]);
Now I use n=49, then the output is
[49 Element Column Vector]
[Data Type: Anything]
[Storage: rectangular]
[Order: Fortran_order]
Why I can't create the symbolic vector [y1;y2;...;y49] in MATLAB? What is my mistake?

Related

Create an nx1 vector that can be changed easily by specifying n

I want to be able to create a simple nx1 vector in which each row value is a constant (e.g. [2 2 ... 2 2]') and also an nx1 vector in which the values in row 1 and row n are specified (e.g. [1 2 2 ... 2 2 1]'). Also, how would you generate a vector in which you are alternating between two values (e.g. [1 -1 1 -1...]')?
Is there anyway to generate these vectors with out manually typing in each value? I tried to find a way to do so by looking through this Matlab documentation, but couldn't work it out. Thank you!
const=2; %desired constant value
len=5; %length of vector
row1=1; %value of row 1
rown=1; %value of row n
x=const*ones(len,1);
y=[row1; x(1:end-2); rown];
Please, try this code.
const=5; %desired absolute value
Len=10; %length of vector
k=1:Len;
a=(-1).^k;
b=const*a;
If you want same absolute number, this code would be ok.
const1=-3; %first value
const2=5; %second value
N=5; %half length of vector
a=const1*ones(1,N);
b=const2*ones(1,N);
k=zeros(1,2*N);
n=1:N;
k(2*n)=a(n);
k(2*n-1)=b(n);
If you want arbitrary two values, please try this code.

Transform a matrix 18x6692 to a matrix 1x120450 matlab

i have a char matrix (in matlab) 18x6692 and i want this to be a matrix with 1 row and 6692x18=120450 column.
I'm not able to do this, can you help me?
I also tried with a smaller matrix: from 2x4 to 1x8 with no results.
thank you
Simply use the colon operator and transpose the vector:
A = A(:).'
You can use the reshape function:
B = reshape(A,1,[]);
where A is the input matrix, 1 is the number of rows and [] is to indicate that the number of columns is to be calculated from the number of elements in A.
Note that this stacks all columns of A. If you want to concatenate along the rows, you can do this by transposing A first
B = reshape(A.',1,[]);

How to store outputs from a function to a matrix in matlab?

so far I have this:
time=(0:15:16*1440);
data=zeros(3,length(time));
for i=1:length(time)
(not sure what goes here)=ValidateTime(0,0,time(i));
end
validateTime is my function that returns 3 values.
How would I store the output from the function into the data matrix I created before?
Assuming ValidateTime(..) returns a row vector of length 3, you can transpose it to a column vector and assign in it to the i'th column in your data matrix.
time=(0:15:16*1440);
data=zeros(3,length(time));
for i=1:length(time)
data(:,i)=ValidateTime(0,0,time(i))'; % Note the single quote!
end

Matlab error while creating a matrix

I want to insert 'double' type values in a matrix. For that I am creating a matrix with following lines of Matlab code:
dpitchcnt=(N/256); %N is total number of byte
pitchvec(1:int64(dpitchcnt)); %creating a matrix 'pitchvec' with 1 row and int64(dpitchcnt)' columns
size(pitchvec) %Trying to display the size.
I am getting the following error while carrying out the above operation:
Undefined function or method '_colonobj' for input arguments of type
'int64'. Error in ==> sample at 31 pitchevec(1:int64(dpitchcnt));
What am I doing wrong?
The syntax varName(1:10) will get the first 10 values of varName, not create the variable varName;
To create a matrix you can use
pitchvec = zeros(1,int64(dpitchcnt)); %A zero-matrix
matrixSize = size(pitchvec);
You can also use ones(n,m);%Create a n times m matrix with 1 all over.

Assigning a vector to a matrix row in MATLAB

I have array (vector) r =[2 4] and report matrix with size 50*2. I want put r in row 1 of report matrix.
When I run
with report(1)=r;
MATLAB returns this error:
??? In an assignment A(I) = B, the number of elements in B and I
must be the same.
How can I resolve this error?
report(1,:)=r
You need to specify the columns as well. Otherwise report(1) is a scalar, r is a vector, and you get an error.
Unless I'm missing something you simply want to have a vector as the first row of a matrix:
report = [r;report]