I want to run turnMatrix(30,X) in MATLAB. I get this error message:
Undefined function or variable 'X'.
If I change X to 1 it is successful. My code is below.
function turnMatrix(d,b)
a = d * pi / 180 ;
if b == X % 1
R = [1 0 0 0 ; 0 cos(a) -sin(a) 0 ; 0 sin(a) cos(a) 0 ; 0 0 0 1];
elseif b == Y % 2
R = [cos(a) 0 sin(a) 0 ; 0 1 0 0 ; -sin(a) 0 cos(a) 0 ; 0 0 0 1];
elseif b == Z % 3
R = [1 0 0 0 ; 0 cos(a) -sin(a) 0 ; 0 sin(a) cos(a) 0 ; 0 0 0 1];
end
R
end
It looks like you are really expecting/wanting to specify the second input as a characters/string, that is
turnMatrix(30,'X')
Note the single quotes around 'X', which say that it is literally the character string 'X', not the value of the variable X.
Then inside the function you need
if b == 'X'
elseif b == 'Y'
elseif b == 'Z'
end
That is, compare the value of variable b to the character 'X'. The way you have it, you are trying to compare the value of variable b to the value of variable X, but variable X doesn't exist and hence the error.
Note also, that if you were comparing anything other than a single character (and even in the single character case) you'd be better off using a switch-case construct rather than if-else
switch b
case 'X'
case 'Y'
case 'Z'
end
Related
I have been studying Matlab for hobby.
This is my second question in stack-overflow.
These days I have interest in Sudoku by Matlab.
I got code from internet and I am studying this code for 3 weeks.
I want to add function which indicate how many times 'sub-function(candidates)' executed. -> how many times function works for solution.
I used to use cnt = 0 and cnt=cnt+1 for count(under if statement and while statement)
but i realize this code's structure is loop(??)
*i tried it but cnt were reset when i try, i think i know the reason of reset
but i can't write it code
Thank you for your help
function X = s2(X)
% SUDOKU Solve Sudoku using recursive backtracking.
% sudoku(X), expects a 9-by-9 array X.
% Fill in all ?singletons?.
% C is a cell array of candidate vectors for each cell.
% s is the first cell, if any, with one candidate.
% e is the first cell, if any, with no candidates.
[C,s,e] = candidates(X);
while ~isempty(s) && isempty(e)
X(s) = C{s};
[C,s,e] = candidates(X);
end
% Return for impossible puzzles.
if ~isempty(e)
return
end
% Recursive backtracking.
if any(X(:) == 0)
Y = X;
z = find(X(:) == 0,1); % The first unfilled cell.
for r = [C{z}] % Iterate over candidates.
X = Y;
X(z) = r; % Insert a tentative value.
X = s2(X); % Recursive call.
if all(X(:) > 0) % Found a solution.
return
end
end
end
% ???????????????
function [C,s,e] = candidates(X)
C = cell(9,9);
tri = #(k) 3*ceil(k/3-1) + (1:3);
for j = 1:9
for i = 1:9
if X(i,j)==0
z = 1:9;
z(nonzeros(X(i,:))) = 0;
z(nonzeros(X(:,j))) = 0;
z(nonzeros(X(tri(i),tri(j)))) = 0;
C{i,j} = nonzeros(z)';
end
end
end
L = cellfun(#length,C); % Number of candidates.
s = find(X==0 & L==1,1);
e = find(X==0 & L==0,1);
end % candidates
end % s2
i used variable X
X=[4 0 0 0 2 0 0 0 0;
0 1 0 3 0 0 5 0 0;
0 0 9 0 0 8 0 6 0;
7 0 0 6 0 0 1 0 0;
0 2 0 0 0 0 0 0 9;
0 0 3 0 0 4 0 0 0;
6 0 0 7 0 0 0 2 0;
0 8 0 0 1 0 0 0 4;
0 0 0 0 0 9 3 0 0];
You could also use a persistent variable, which should retain its value across multiple calls of the function.
For example:
function [ result ] = myfactorial( num )
% using persistent variable based on
% https://www.mathworks.com/help/matlab/ref/persistent.html
persistent numcalls;
if isempty(numcalls)
numcalls = 0;
end
numcalls = numcalls+1;
disp(strcat('numcalls is now: ', int2str(numcalls)));
% factorial function based on
% https://www.quora.com/How-can-I-create-a-factorial-function-in-MATLAB
if num > 1
result = myfactorial(num-1)*num;
else
result = 1;
end
end
Now call the function.
clear all;
result = myfactorial(5);
The output is:
numcalls is now:1
numcalls is now:2
numcalls is now:3
numcalls is now:4
numcalls is now:5
function [w]=example3(v)
w=[0];
for x =v
for y=v
w(x,y)=x+y;
end
end
end
An example:
v=[5 2];
[w]=example3(v)
0 0 0 0 0
0 4 0 0 7
0 0 0 0 0
0 0 0 0 0
0 7 0 0 10
I have this code and I'm trying to figure out how often does the value change for w. More than that though, I want to , step by step, how the value of w change (maybe just the first few iterations of it).
As suggested by others, you can define a break point where you assign a value to w and see its value.
If you want to do it programmatically, you could add this:
function [w]=example3(v)
w=[0];
idx = 0; % Count number of cycles
for x =v
for y=v
w(x,y)=x+y;
idx = idx + 1;
if idx < 6 % Only display first 5 values
disp(['x: ' num2str(x) ' - y: ' ...
num2str(y) ' - w: ' num2str(w(x,y))])
end
end
end
end
let us consider following code for impulse function
function y=impulse_function(n);
y=0;
if n==0
y=1;
end
end
this code
>> n=-2:2;
>> i=1:length(n);
>> f(i)=impulse_function(n(i));
>>
returns result
f
f =
0 0 0 0 0
while this code
>> n=-2:2;
>> for i=1:length(n);
f(i)=impulse_function(n(i));
end
>> f
f =
0 0 1 0 0
in both case i is 1 2 3 4 5,what is different?
Your function is not defined to handle vector input.
Modify your impluse function as follows:
function y=impulse_function(n)
[a b]=size(n);
y=zeros(a,b);
y(n==0)=1;
end
In your definition of impulse_function, whole array is compared to zero and return value is only a single number instead of a vector.
In the first case you are comparing an array to the value 0. This will give the result [0 0 1 0 0], which is not a simple true or false. So the statement y = 0; will not get executed and f will be [0 0 0 0 0] as shown.
In the second you are iterating through the array value by value and passing it to the function. Since the array contains the value 0, then you will get 1 back from the function in the print out of f (or [0 0 1 0 0], which is an impulse).
You'll need to modify your function to take array inputs.
Perhaps this example will clarify the issue further:
cond = 0;
if cond == 0
disp(cond) % This will print 0 since 0 == 0
end
cond = 1;
if cond == 0
disp(cond) % This won't print since since 1 ~= 0 (not equal)
end
cond = [-2 -1 0 1 2];
if cond == 0
disp(cond) % This won't print since since [-2 -1 0 1 2] ~= 0 (not equal)
end
You could define your impulse function simply as this one -
impulse_function = #(n) (1:numel(n)).*n==0
Sample run -
>> n = -6:4
n =
-6 -5 -4 -3 -2 -1 0 1 2 3 4
>> out = impulse_function(n)
out =
0 0 0 0 0 0 1 0 0 0 0
Plot code -
plot(n,out,'o') %// data points
hold on
line([0 0],[1 0]) %// impulse point
Plot result -
You can write an even simpler function:
function y=impulse_function(n);
y = n==0;
Note that this will return y as a type logical array but that should not affect later numerical computations.
If I know that K = kron ( A , B ),where kron is the Kronecker product of matrices as defined in MATLAB, how can I find A and B from a given matrix K?
for example suppose that
K = [1 0 0 0 ;0 1 0 0 ; 0 1 0 0 ;0 1 0 0 ;1 0 0 0 ;0 1 0 0 ;0 1 0 0 ;0 1 0 0 ;1 0 0 0 ;0 1 0 0 ;0 1 0 0 ;0 1 0 0 ;0 0 1 0 ;0 0 0 1 ;0 0 0 1 ;0 0 0 1 ]
is there any way to find A and B, such that K = kron ( A , B )?
In this case, A and B are as follows:
A = [ 1 0 ; 1 0 ; 1 0 ; 0 1 ]
B = [ 1 0 ; 0 1 ; 0 1 ; 0 1 ]
Short Discussion and Solution Code
You can't find both A and B, given a K, because there could be many possible A's and B's to produce a certain kron matrix, K. Thus, alongwith K, you need either A or B to get the remaining input B or A respectively.
Case #1 Given A and K (kron matrix), you can find B with -
B = K(1:size(K,1)/size(A,1),1:size(K,2)/size(A,2))./A(1)
Case #2 Given B and K (kron matrix), you can find A with -
A = K(1:size(B,1):end,1:size(B,2):end)./B(1)
Thus, if not the entire other input, you would at least need to know the size of it and one of the elements of it, preferably the first element.
Function Code
You can very easily convert that to a function code for an easy plug-n-play usage -
%// INVERSE_KRON Inverse of Kronecker tensor product to find one of the inputs.
% // INVERSE_KRON(K,ARRAY,INPUT_ID) finds one of the inputs used for calculating the
% // Kronecker tensor product given the other input and the ID of that other input.
% // Thus, if K was obtained with K = KRON(A,B), one must use -
% // A = INVERSE_KRON(K,B,2) to find A, and
% // B = INVERSE_KRON(K,A,1) to find B.
function out = inverse_kron(K,array,input_id)
switch input_id
case 1
out = K(1:size(K,1)/size(array,1),1:size(K,2)/size(array,2))./array(1);
case 2
out = K(1:size(array,1):end,1:size(array,2):end)./array(1);
otherwise
error('The Input ID must be either 1 or 2')
end
return;
Typical use case would look like this -
K = kron(A,B); %// Get kron product
A = inverse_kron(K,B,2) %// Recover A
B = inverse_kron(K,A,1) %// Recover B
Note: For a vector case, one other related question and answer could be found here.
I've started out with MATLAB a few hours ago and began with plotting some simple functions, e.g.:
k = -20:20;
a = 0.01;
x_k = exp(-a * k.^2)
f_1 = figure(1);
plot(k, x_k)
This works fine and plots each value of k on the x-axis, with its corresponding function response at the y-axis. Now I've defined my own function:
function [y] = triangle(t, a)
if (a <= t) & (t <= 0)
y = (1/a * t + 1) * a
elseif (0 <= t) & (t <= a)
y = (-1/a * t + 1) * a
else
error('Function not defined for these values')
end
end
I now want to plot triangle for a range of t with a specific parameter a. So I call:
a = 1;
int = (2 * a) / 20;
t = -a:int:a;
f_1 = figure(1);
plot(t, triangle(t, a))
… but my function errors out because the entire t array (vector, I suppose?) is passed to it instead of each element of t.
How do I plot my triangle function for a specific a, over a defined range?
I've also tried fplot(triangle(a), t) (since fplot accepts a function as an argument) but that again complains about triangle not receiving enough input arguments—rightly so, but I need to pass a to it as well.
The problem of this is the that you pass a vector to your function triangle but you want to check every single if-statement for each single value of the vetor t. So either you use a for-loop within the function triangle or you could also do it by using this:
function [y] = triangle(t, a)
y = ((a <= t) & (t <= 0)) .* ((1/a * t + 1) * a) + ((0 <= t) & (t <= a)) .* ((-1/a * t + 1) * a);
end
I know it looks somehow weird, but if you need me to explain it a bif more in detail, I can do this for you ;-)
EDIT: Let's shortly examine the problem: You pass a vector t to the function, which means that the first if statement if (a <= t) & (t <= 0) splitted looks like this:
(a <= t) % resulting in:
ans =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
(t <= 0) % resulting in:
ans =
1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
so combining them via the AND operator (& for vectors), this obviously gives you
ans =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
So this branch wont be executed. The elseif gives you:
(0 <= t)
ans =
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
(t <= a)
ans =
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
((0 <= t) & (t <= a))
ans =
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
so this won't be executed either even though you'd want it to be executed for the second half of your values because you'd like to check the two statements for each value-pair on it's own.
So by leaving away the statements completely you could do it like I posted above: simply multiply the result of the logical statements elementwise with the piecewise function which will yield the result you want to calculate :-)
The best approach is to define the triangle function so that it admits vector inputs. You and do it with logical indexing, which selects a range of values of t depending on a logical condition:
function [y] = triangle(t, a)
y = NaN(size(t)); %// pre-allocate and define NaN as default return value
ind = (a <= t) & (t <= 0); %// select first range
y(ind) = (1/a * t(ind) + 1) * a; %//compute output for first range
ind = (0 <= t) & (t <= a); %// select second range
y(ind) = (-1/a * t(ind) + 1) * a; %//compute output for second range
end
This returns NaN for out-of-range values. As an example, for your t,
>> triangle(t, a)
ans =
Columns 1 through 13
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0000 0.9000 0.8000
Columns 14 through 21
0.7000 0.6000 0.5000 0.4000 0.3000 0.2000 0.1000 0
Once you define the function like this, you can do plot(t, triangle(t, a)).
To pass your function to fplot, you need to use an anonymous function (like what is called a lambda in many other programming languages).
This is done with the # syntax in matlab (see help function_handle for how to use this).
So your fplot try should be:
fplot(#(x) triangle(x,a), t([1 end]))
Note that the t input for fplot is the bounds in which you want to evaluate your function, not the values to evaluate itself (you can use bounds in combination with fplot's third input argument for some control over that).
That said, as other posters also noted, the part of your example input where t<0 end up right in your else branch and yields an error.