Matlab error while creating a matrix - matlab

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.

Related

Error in Matlab Function Block: Index exceeds array dimensions

I have some problem during running the code below and it give me the error
Index exceeds array dimensions. Index value 3 exceeds valid range [1-2] for array 'a'. Error in 'TEST/TEST' (line 18) if a(i)> 0
This code is the code for computing the parameter k for the scalar reference governor. and Hx and Hv are the maximal admissible output sets(MAS) with the matrices A,B,C,D hope can get some help to fix this code from your all.
function v = SRG_new(v_previous, r)
A=[0 1;-275.5 -21.22];
B=[0;1];
C=[11.02 275.5];
D=0;
I=eye(2);
Hx=(C*A);
Hv= C*((I-A)*((I-A)^-1)*B+D);
s=350; %s=max_output
a=Hx*(r-v_previous);
b=s-Hx-Hv*v_previous;
k=1;
for i=1:100
if a(i)> 0
k=min(k, b(i)/a(i));
end
end
k=max(k,0);
v=v_previous + k*(r-v_previous);
end
Well this depends mainly on the size your inputs v_previous and r (As The error specifies the range "1-2" i guess, "r" and "v_previous" are scalar values).
The error occurs, because you want to iterate through 100 elements of "a", but as Hx=(C*A); only creates a 1x2 Matrix, a = Hx*(r-v_previous); will result in a Matrix "a" with fewer than 100 entries (Exactly two entries if only multiplied with a scalar value). And this must lead to the "Out of range - Error".
To get rid of the error, simply use
for i=1:numel(a)
if a(i)> 0
k=min(k, b(i)/a(i));
end
end
This way, it will only iterate through the available array elements.

Error in creating a volcano plot in MATLAB

I am a complete newbie to MATLAB and the first task I have is to create a volcano plot. I have been using the documentation to understand about it and get started.
I tried to run it on dummy values -
a=[1 2 3]
b=[4.6 2.7 4.5]
c=[0.05 0.33 0.45]
And then I ran -
SigStructure = mavolcanoplot(a, b, c)
My understanding is that a represents the gene expression values for condition 1, b for condition 2, and c is the list of p-values for the 3 data points in a and b.
However running this code gives me the error -
Index exceeds matrix dimensions.
Error in mavolcanoplot (line 127)
appdata.effect = X(paramStruct.goodVals) - Y(paramStruct.goodVals);
Error in volc (line 4)
SigStructure = mavolcanoplot(a, b, c)
Can anyone explain where I am going wrong?
You're encountering an issue because you are using row vectors.
Inside the mavolcanoplot function (you can see the file by typing edit mavolcanoplot in the command window) there is a local function for checking the inputs, called check_inputdata.
Your data passes all of the validation checks, and then encounters this section:
% Here, 'X' and 'Y' are the local names for your inputs 'a' and 'b'
% Below code is directly from mavolcanoplot.m:
% Handle the matrix input. Use its mean values per row
if size(X, 2) > 1
X = mean(X,2);
end
if size(Y, 2) > 1
Y = mean(Y,2);
end
This reduces your inputs to their mean. Later in the main function (line 127) you encounter the error as described, where paramStruct.goodVals is a 3 element array, which is now trying to index a 1 element array and fails!
This is basically a lesson in debugging and reading the documentation, which states
DataX, DataY: If a [...] matrix, each row is a gene, each column is a sample and an average expression value is calculated for each gene.
You should use one of these equivalent methods to create column vector inputs
a=[1 2 3].'; % Using transpose (.') to create a column vector from a row vector
b=[4.6; 2.7; 4.5]; % Creating a column vector using the semi-colon operator to end each row
c=[0.05
0.33
0.45]; % Using actual code layout to create a column vector

Sum of all values in matrix (Matlab)

So I'm trying to create a matlab function that takes two inputs, a matrix and a value, and returns the sum of all values in the matrix except for all instances of the given value. So far this is the code I have written:
function [total] = sumAllExcept(matrix, except)
total = 0;
for i = 1:size(matrix, 1)
for k = 1:size(matrix, 2)
if(matrix(i, k) ~= except)
total = total + matrix(i,k);
end
end
end
end
The error message that I am receiving when trying to run the program is:
" Undefined function 'sumAllExcept' for input arguments of type 'double'. "
I would greatly appreciate it if you would show me whats wrong with this and fix anything that you can. Thank you!
Sum the array after filtering out except using logical indexing:
total = sum(matrix( matrix ~= except ));
The result of using the logical index matrix ~= except on matrix returns a column vector, so only one sum is required.
The error "Undefined function 'sumAllExcept' for input arguments of type 'double'." is likely due to the function not being on MATLAB's path or the function name sumAllExcept not matching the filename (i.e., sumAllExcept.m).

Matlab Function with Varying parameters

I need help figuring out how to code the following problem. Any help would be greatly appreciated!
Create a function that will take a vector/array input for x (1 by n) and a scalar input for a, and produce the output defined by the following equation:
y(x,a)=((xsin(ax-2))/(sqrt(1+(ax)^2)
-π ≤ x ≤ π
a={.5 1 1.5 2}
The equation must be vectorized in terms of x and the output from the function is the array y which has the same dimension as the array x.
Write a script that calls this function to compute y(x,a) for the range of x defined above and each value of the parameter a. Results should be stored in a solution matrix using a different row of the solution matrix for each value of a.
So far for my function I have:
function [y] = part1(a,x)
y=((x*sin(a*x-2))/(sqrt(1+(a*x).^2)));
end
I'm not sure how to output this into the solution matrix
For my script I have:
%%
clear,clc
a={0.5 1 1.5 2};
x=-pi:0.1:pi;
for
part1(x,a)
end
I'm getting the following errors when I run this now:
Undefined function 'mtimes' for input arguments of type 'cell'.
Error in part1 (line 4)
y=((x*sin(a*x-2))/(sqrt(1+(a*x).^2)));
Error in labtest2 (line 8)
y(i,:)=part1(x,a(i));
EDIT
I've made some changes and am still getting some errors that I cannot resolve.
Here is my full code for function followed by full code for script:
Function
function [y] = part1(x,a)
nx=numel(x);
na=numel(a);
y=((x.*sin(a.*x-2))./(sqrt(1+(a.*x).^2)));
size(y)=[nx na]
end
Script
%%
clear,clc
a={0.5 1 1.5 2};
x=-pi:0.1:pi;
for i = 1:length(a)
y(i,:)=part1(x,a(i));
end
Errors
Undefined function 'times' for input arguments of type 'cell'.
Error in part1 (line 6)
y=((x.*sin(a.*x-2))./(sqrt(1+(a.*x).^2)));
Error in labtest2 (line 8)
y(i,:)=part1(x,a(i));
The reason you're getting Undefined function 'times' for input arguments of type 'cell' is because your variable a is a cell array. You need to change your assignment of a from
a={0.5 1 1.5 2};
to
a=[0.5 1 1.5 2];
which will make it just a normal array. Alternatively, you need to reference it with cell array notation: a{i} instead of a(i).
You're almost there. Note that you've written
function [y] = part1(a,x)
but you call it in your script as
part1(x,a)
so you should probably correct that.
A few things jump out at me:
You never assign the output of part1(x,a) to anything. You're told that
Results should be stored in a solution matrix using a different row of the solution matrix for each value of a.
What I take this to mean is that the 1st row corresponds to part1() evaluated for the 1st element of a. Since we're operating on x which is a vector, that row will have multiple columns. Your output is indeed a matrix. In your case, length(-pi:0.1:pi) == 63, therefore size(y) == [4 63], where y is your output matrix.
Your for loop is backwards. You're told to accept scalar a and vector x. Therefore, your script should be something like:
a = 0.5:0.5:2;
x = -pi:0.1:pi;
for i = 1:length(a)
y(i,:) = part1(x, a(i));
end
Note the use of length and the : operator. I want to iterate between 1 to length(a) (in this case, length(a) == 4) so that I can use the current a(i) value as an index into my output matrix, y. The : operator in y(i,:) signifies "The ith row and all columns of y will take the value output by part1(x,a(i))."
Your function needs to be changed up for element-by-element operations. Notice that, for instance, x*sin(a*x-2) works for scalar x but not vectors. This is because x is a vector and sin(a*x-2) is also a vector (since the sin call will operate element-by-element and a is a scalar). Trying to multiply two vectors together will result in errors since MATLAB will try to perform a matrix multiplication. Resolve this by replacing * with .*. This way it is unambiguous that you are going to multiply these two vectors element-by-element. You'll also need to change / to ./.
On another note, thank you for attempting to do your homework before asking SO for help. We've been getting a huge influx of questions from students that have made no attempt to do their own work before dumping it on us, so it's refreshing that we regulars of the MATLAB tag get to actual help out instead of telling people to do their own work.
Finally got the whole thing worked out.
Function
function [y] = part1(x,a)
y=((x.*sin(a.*x - 2))./(sqrt(1 + (a.*x).^2)));
end
Script
%%
clear all;
clc;
close all;
x=[-pi:.1:pi];
a=[.5:.5:2];
for i=1:length(a)
y(i,:)=part1(x,a(i));
plot(x,y)
end
Sol=[y]

Subscript indices must either be real positive integers or logicals

I write a function to sum each row of a matrix which have three rows.
Then use a matrix which have one row and three columns to divide the previous result.
But I keep getting that error. I know the subscript should not be a decimal or negative number. But I still can not find the culprit. Please help, thanks.
% mean_access_time(ipinfo_dist, [306, 32, 192])
% 'ipinfo_dist' is a matrix which have three rows and column is not fixed.
function result = mean_access_time(hash_mat, element_num)
access_time_sum = sum(rot90(hash_mat));
result = bsxfun (#rdivide, access_time_sum, element_num);
For example:
A=
1 2
3 4
5 6
B= 7 8 9
Then I want to get
[(1+2)/7, (3+4)/8, (5+6)/9]
Update:
>> which rot90
/lou/matlab/toolbox/matlab/elmat/rot90.m
>> which sum
built-in (/lou/matlab/toolbox/matlab/datafun/#uint8/sum) % uint8 method
Culprit:
I used mean_access_time as a variable in the previous command line.
It seems like you have overridden a built-in function ( rot90 or sum ) with variable name.
Type
>> dbstop if error
And run your code.
When the error occurs type
K>> which rot90
K>> which sum
See if you get a built-in function or a variable name.