Which part of this is a non-negative integer? (factorial error) [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This is the script:
n=input('Enter the number of rows: ')
PT=zeros(n);
row=1;
col=1;
while row~=n+1
for col=1:1:n
PT(row, col)=(factorial(row-1)/(factorial(col-1)*factorial(row-col)));
end
row=row+1;
col=1;
end
PT
When I run it, it says to enter the number of rows, so I enter '4'. Then it says
error: factorial: all N must be real non-negative integers
error: called from
factorial at line 40 column 5
hw6p2 at line 7 column 17
I don't understand what's wrong.

In the line for col=1:1:n, col can get up to the value n, even when row is still 1. Therefore when you call factorial(row-col), row can be 1 and col can be 2. This is where it can be negative.

Your factorial input should not be negative .....in the loop (row-col) is taking a negative value and error popped. Use abs to make it positive always.
n=input('Enter the number of rows: ')
PT=zeros(n);
row=1;
col=1;
while row~=n+1
for col=1:1:n
PT(row, col)=(factorial(row-1)/(factorial(col-1)*factorial(abs(row-col))));
end
row=row+1;
col=1;
end
PT

Related

Generate a 10-digit random number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to generate a random number with exactly 10-digits.
The random number cannot begin with zero, i.e. it must be a 10 digit number, not 10 random digits.
You need a random integer, between 1000000000 (the lowest integer with 10 digits) and 9999999999 (the highest number with 10 digits).
Note that 1000000000 = 1e9 and 9999999999 = 1e10 - 1
The random integer generation can easily achieved with randi (see the documentation here), giving it the correct minimum and maximum values...
n = randi([1e9, 1e10-1])

How can I trace the highest value in a matrix until I go to a cell in the matrix with zero value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to start with the highest value in the matrix (i,j). Then, go backwards to one of positions (i-1,j), (i, j-1), and (i-1, j-1) depending on the direction of movement used to construct the matrix. This method is used throughout until a matrix cell with zero value is reached.The above image shows what I want, I need to trace those marked in blue. I know there is a max function in matlab.
If I understand what you try to do...
Suppose M is your matrix:
result=[];
[~,ind]=max(M(:));
[r,c]=ind2sub(size(M),ind);
result=[result; r c];
while M(r,c)~=0
[~,f]=max([M(r-1,c),M(r,c-1),M(r-1,c-1)]);
switch f
case 1
r=r-1;
case 2
c=c-1;
case 3
r=r-1;
c=c-1;
end
result=[result; r c];
end
Then, result is a 2 x n matrix, where each row is the indexes of one step- from the upper to lower.

Is colon notation (:) equal to array(vector) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have wrote a small program to test some funnction.
This is the proram:
close all;
clear all;
f = #(x, n) power(-1,(n-1)./2) .* power(x, n) ./ factorial(n);
n = [0,3,5,10,50,100];
% n = 0:10:100;
x = linspace(0, 4*pi, 1000);
ax = axes('nextplot', 'add');
for k = 1:length(n)
plot(ax, x, f(x, n(k)), 'displayname', ['f_', int2str(n(k)), '(x)']);
end
The main problem for me is that I thought that colon notation(1:10) definies array with values equaliy spread.
In my program that is not the case.
There is difference in output of the program when I set n as [0,3,5,10,50,100] and if I set n as 0:10:100.
In the first version, with array, the program works fine, but wit the second version, with colon notation, the program does not work it simply draws a line at 0.
So my question is way this is happening? I mean if the colon notation and the array definition are the same why does the program has different output for the colon notation and array notation?
Did I missunderstod something?
Thanks!!
EDIT:
This are the plots that I get:
First id array notation, second is colon notation.
I am using mathlab R2013a
In your commented line,
% n=[0:10:100];
you create a vector with values from 0 to 100, with spacing 10, ie [0 10 20 30 40 50 60 70 80 90 100].
With your uncommented line,
n=[0 3 5 10 50 100];
you have the values you specify.
Since they're not the same input, you won't get identical output.

MATLAB: Modulus Function for Summing Odd Integers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would you use the modulus function to sum odd integers?
I was able to view the odd integers but couldn't sum them together.
If I am interpreting your question correctly, you don't want to use the modulus function to sum odd integers, but you want to use the modulus function to help you determine what is an odd integer and sum only those numbers in your data set that are odd integers. If you read this at a first glance, this is confusing because it sounds like you want to use the modulus function and the modulus function only to sum values.
Let's say you have integers in a vector called data. What you can do is if you want to check to see whether an integer is odd, you check to see if the remainder once you divide by 2 is 1 (i.e. num mod 2 == 1). Recall the definition of an odd number. This means that you can take any integer, and represent it such that you can find an integer k where the number can be represented as 2k + 1. Therefore, if you were to take the modulus of this number with 2 as the base, you would get 1 as the answer. Bear in mind that this does not apply to negative numbers.
As such, these are the steps that I would suggest that you do:
Find all of the locations where the modulo function gives you 1.
Use these locations and sum up your data.
Here is the code I would use:
function sumOdd = sumOddNumbers(data)
%// Step #1
ind = mod(data, 2) == 1;
%// Step #2
sumOdd = sum(data(ind));
Here we are creating a function that will help us do that. You specify data as input into your function, and the output (sumOdd) will contain the sum of the odd numbers. As such, take this code and save it to a new .m file. Also, make sure you call it sumOddNumbers.m. Next, make sure you set your working directory to be where you have placed this file.
Let's do a quick example:
data = 1 : 10; %// Create an array going from 1 to 10
sumOdd = sumOddNumbers(data); %// Answer should be 25... why?
Now let's go through the function step by step and see why the answer is 25.
ind = mod(data, 2) == 1; %// Should give us an array s.t. [1 0 1 0 1 0 1 0 1 0]
sumOdd = sum(data(ind)); %// Should sum over the following array [1 3 5 7 9]
%// Answer is 25

Returning the last element of a vector with an unknown length [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I retrieve the last element of a vector only, provided that the length of that vector is unknown?
Use the special end keyword:
lastelement = myvector(end);
If the vector is called A, just use A(end).
In this case, use end, like #nispio and #David answered.
But it seems you think that not knowing the length can be a problem, but nope. That's because you can use length(v) if v is a column or row vector, or size(M) if M is a matrix.
Then, to get the last element of your vector, you could use (not recommended):
v(length(v)) if v is a row or a column vector
v(size(v,1)) if v is a column vector
v(size(v,2)) if v is a row vector
But if you use one of them, MATLAB will warn you:
The operation or expression <Indexing> has no evident effect.