Sum of all values in matrix (Matlab) - 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).

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.

Matrix as input and output of Matlab function

For example I want to have a function which let me delete rows of my matrix where the highest value is 1. Thus I wrote:
% A is an input matrix
% pict2 suppose to be output cleared matrix
function pict2 = clear_(A)
B=A
n = size(A,1)
for i=1:n
if(max(B(i,:))==1)
B(i,:)=[]
end
end
But after I call:
pict2=clear_(pict) the Matlab resposes:
"warning: clear_: some elements in list of return values are undefined
warning: called from
clear_ at line 5 column 1 pict2 = "
I'm not sure which elements were left undefined?
The variable name of your output argument must match the variable that you want to be returned. So you'll want to change the first line to the following so that your modifications to B are saved and returned.
function B = clear_(A)
As far as your algorithm is concerned, it's not going to work because you are modifying B while trying to loop through it. Instead, you can replace your entire function with the following expression which computes the maximum value of each row, then determines if this value is equal to 1 and removes the rows where this is the case.
B(max(B, [], 2) == 1, :) == [];
I believe that, alternatively to the suggestions you already recieved, you might want to try the following. Using logicals is probably one of the best options for such a problem, since you don't need to use for-loops:
function out = clear_matr(A)
% ind is true for all the rows of A, where the highest value is not equal to 1
ind = ~(max(A, [], 2) == 1);
% filter A accordingly
out = A(ind, :);
end

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]

Summing a cell array?

I have a function that takes in any number of arguments into a cell array
function sumThese(varargin)
subtotals = cellfun(#sum, varargin);
total = sum(subtotals);
disp(total)
end
This works for arrays and numbers, except as soon as I have a square matrix it doesn't. It'll tell me:
Non-scalar in Uniform output, set 'UniformOutput' to false.
However if I set 'uniformoutput' to false, I get this error now:
Undefined function or method 'sum' for input arguments of type 'cell
How to approach this?
Change the function #sum in the cellfun
subtotals = cellfun( #(x) sum(x(:)), varargin );
Why?
becuase the output of sum, when applied to a matrix is no longer a scalar what turns subtotals into a cell array of scalars and vectors, instead of a 1D vector.
Use debugger to see the difference.
PS,
Did you know that cellfun is not always better than a simple loop.
EDIT:
A solution using for loop:
total = 0;
for ii = 1:numel(varargin)
total = total + sum( varargin{ii}(:) );
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.