Subscript indices must either be real positive integers or logicals - matlab

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.

Related

Can you explain what this line does in Matlab

I'm new to Matlab and I'm just stuck with this line of code:
[r,c] = find(abs(fh) <= 2 );
Beware: ironically it was easy for me understanding what the right part of the assignment is.
The left part however (which is maybe the definition of a variable)... I don't know how to search because I have too generic results by googling just square brackets with something inside.
My assumption is this line of code is creating some a matrix with r rows and c columns but r and c are nowhere to be found in the rest of the code.... or maybe it's a simple array with two elements... but it doesn't make much sense to me honestly.
Can you guys help me please?
Whenever you see that syntax, it means that the function being called is returning more than one output argument (two in this case).
The best way to learn about the function output arguments is to check the documentation:
https://www.mathworks.com/help/matlab/ref/find.html#d120e368337
[row,col] = find(___) returns the row and column subscripts of each
nonzero element in array X using any of the input arguments in
previous syntaxes.
The output arguments are positional, so r is row, c is col.
Take a look in Matlab find() docs.
If X is a vector, then find returns a vector with the same orientation as X.
If X is a multidimensional array, then find returns a column vector of the linear indices of the result.
If X contains no nonzero elements or is empty, then find returns an empty array.
If you call
X = [18 3 1 11;
8 10 11 3;
9 14 6 1;
4 3 15 21 ]
[row,col] = find(X>0 & X<10,3)
You will get:
row = 3×1
2
3
4
col = 3×1
1
1
1
Which represents the index (row number and column number) of each elements that satifies the condition you defined. Since it returns more than 1 value, you can divide the output in two different variables and that is what the left side represents.

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

Results not correct when using diff function in Matlab

I'm using the diff Matlab function to get the difference between two successive values. And as shown here in this vector nz in this link as shown in nz the difference between col 261 and 260 is -1342 but when I use this script the result of difference between this coloumns don't appear in the result dnz. So if anyone could advise why this is not working?
This is my attempt:
load('nz.mat');
dnz = diff(nz);
If you type class(nz) you see that your data is unit16. And MATLAB saturates the results when dealing with integer values, i.e. since 0 - 1342 is lower than zero (the smallest value in uint16) it returns zero:
>> dnz=diff(nz);
>> dnz(260)
ans =
0
If you convert it to a class that can accomodate -1342 like int16 you get
>> dnz = diff(int16(nz));
>> dnz(260)
ans =
-1342

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]

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.