Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to sum an array the array maybe will be 10x10 or 2x2 or 3x3
1 2 3
1 2 3
1 2 3
i want to sum the Inverted digonal start from [1,3] end [3,1] but we want to consider that the length of the array could change.
Something like this should do it (Assuming your Array is called x):
Dim Sum As Double = 0
For i = 0 To UBound(x, 2)
Sum += x(UBound(x, 2) - i, i)
Next
Assuming that your array will always be square (i.e. 2x2, 6x6, 200x200, etc) then the following pseudo-code will produce the result you are after:
x = [square array]
i = 0
j = x.length - 1
sum = 0
while (i < x.length)
sum += x[j--][i++]
Related
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
Based on data (e.g. gender,age and handedness) of 8 participants of an experiment I am required to store the information in a 2-dimensional matrix using MATLAB Programming and correct a “mistake” by changing the handedness of two participants. Say:
% id, gender, age, handedness
participants = [1 1 21 1;
2 2 25 1;
3 2 19 1;
4 2 23 2;
5 1 23 1].
Can someone help me please?
You're showing a 5x4 matrix:
% id, gender, age, handedness
participants = [1 1 21 1;
2 2 25 1;
3 2 19 1;
4 2 23 2;
5 1 23 1];
In Matlab you can access cells in a matrix with the () operator. In your case your 2D matrix you have (row,column). E.g. the age of the second participant would be
display(participants(2,3)); % 25
You can show all rows or columns with : e.g.
display(participants(2,:)); % 2 2 25 1
will display all information for the second participant whereas
display(participants(:,3)); % 21; 25; 19; 23; 23
will display the third column "age".
If you want to modify a cell you can do it like this:
participants(2,3) = 99
This would change the age of the second participant to 99.
hth
If I understood you correctly, probably what you want to do is to estimate (e.g., with OLS) the handedness as a function of the other two variables and project on the fitted hyperplane, to find the handedness values that gives the largest errors. We can do the same with the following:
X = participants(:,2:3); % explanatory variables or regressors
y = participants(:,4); % the dependent variable handedness
w = inv(X'*X)*(X'*y); % weights learnt for OLS
y_hat = X*w % predicted handness
% 0.97038
% 1.41533
% 1.22988
% 1.35351
% 1.03220
e = abs(y - X*w) % errors in prediction
% 0.029620
% 0.415325
% 0.229878
% 0.646491
% 0.032196
As we can see, the two handedness values with maximum errors (mistakes) are the 2nd and the 4th value, which are respectively 1 and 2 in the sample data. So they are the ones with maximum mistakes and can be corrected.
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 6 years ago.
Improve this question
if i have a=[1 2 3 4;2 3 4 0;1 1 3 4;0 0 0 1] and I want to find rows at least contain 3 element of [1 2 3 4],for instance rows 1,2,3. what can I do it?
thanks in advance
Use bsxfun as follows:
a=[1 2 3 4;5 6 7 1;2 3 1 7;3 1 1 2]; % matrix
v = [1 2 3]; % desired values
ind = find(all(any(bsxfun(#eq, a, reshape(v,1,1,[])), 2), 3));
This reshapes v into the third dimension (reshape(v,1,1,[])) and compares all values of a with all values of v (bsxfun(#eq, ...)). Then
it selects indices (find(...)) of rows such that all desired values (all(..., 3)) are present in any of the entries of that row (any(..., 2)).
To find the row indices that have at least three elements from array v, you can sum up the counts corresponding to each row and then perform the detection:
n = 3
ind = find(sum(any(bsxfun(#eq, a, reshape(v,1,1,[])), 2), 3)>=n)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Given an array[] of positive integers and another integer k, I have to find number of subset whose sum is multiple of k(sum is evenly divisible by k).
For example,
array[] = {1, 2, 3, 4}, k = 3
Subset sums are,
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
2 = 2
2 + 3 = 5
2 + 3 + 4 = 9
3 = 3
3 + 4 = 7
4 = 4
Thus, {3, 6, 9} are multiple of k = 3 and the answer is 3. For the same array above and k = 2, answer will be 4 = {6, 10, 2, 4}
How to implement it efficiently for array size 1 million.
This is a close variant of Subset Sum Problem, and as the original, it is NP-Complete (Reduction from Partition Problem is trivial).
It can be solved using Dynamic Programming by following the recursive formulas:
D(0,0) = true
D(0,x) = false x > 0
D(i,x) = false x < 0
D(i,x) = D(i-1,x) OR D(i-1,x-arr[i])
In here, D(i,x) is true if and only if you can use a subset of the first i elements to build the number x.
This can be calculated efficiently using Dynamic Programming.
When you are done, simply count the number of values of i such that D(n,k*i) = true
This will take O(n*W) time where n is the number of elements, and W is the sum of them.
This seems like a clear use of using recursion.
for each value in array
test value by itself
test all combinations of remaining array, both with value added and without value added.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
This is a question about theoretical computing. I have came through a question like below;
Consider a project with the following functional units :
Number of user inputs = 50
Number of user outputs = 40
Number of user enquiries = 35
Number of user files = 06
Number of external interfaces = 04
Assuming all complexity adjustment factors and weighing factors as average, the function points for the project will be;
The answer is 672. How is this calculated?
1. Typical complexity averages are as follows:
AVERAGE complexity weights = {4, 5, 4, 10, 7} for the 5 complexities respectively.
2. Typical Characteristic weights are as follows:
AVERAGE characteristic weight = 3.
3. Function point = FP = UFP x VAF
UFP = Sum of all the complexities i.e. the 5 parameters provided in the question,
VAF = Value added Factor i.e. 0.65 + (0.01 * TDI),
TDI = Total Degree of Influence of the 14 General System Characteristics.
Thus function points can be calculated as:
= (200 + 200 + 140 + 60 + 28) x (0.65 + (0.01 x (14 x 3))
= 628 x (0.65 + 0.42)
= 628 x (1.07)
= 672
Thus the function points for the project will be 672.
Checkout this article for a detailed walk-through into function-point calculations.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a problem. I want to find factorial of big numbers.
Ex: 1555! = ?.
195! = ?.
My main problem is that I want to know the exact number of ending 0's of the factorial numbers.
I use the following formula:
(m!)^n = m! = 2*10^(n-1) + 2^2 * 10^(n-2) + ------- + 2^n.
with this I can solve the other factorials for number of ending 0's like this.
100!= 2*10^1 + 2^2*10^0 = 20+4 = 24
100! has 24 ending 0's as per this calculation.
But, then I got other problem,
Ex: For 95!
i) 95! = (100 - 5)! = 24 - 2*5^(1-1) = 24 - 2 = 22 => 95! has 22 0's.
ii) 95! = (90 + 5)! = 9*(2*10^0) + 2*5^0)= 18+2 = 20 => 95! has 20 0's.
this is my problem. By using the above formula I got two different answers and I am confused, I don't get the perfect answer so please help me to find it.
Thank you...
The number of trailing zeros in n! is the number of factors of 5 in the sequence 1, 2, ..., n. This is because a trailing zeros is the number of factors of 10 in the result, and 10 has a prime factorisation of 5 x 2. There's always more factors of 2 than 5, so the number of 5's gives the result.
The number of factors of 5 is... [n/5] + [n/25] + ... + [n/(5^k)] + ... where [ ] means round down (floor).
What should the code look like to compute this? Something like this perhaps.
int trailing_factorial_zeros(int n) {
int result = 0;
int m5 = 5;
while (n >= m5) {
result += n / m5;
m5 *= 5;
}
return result;
}
This is a bad question, probably belongs on Math site anyway. But here's a thought for you:
First 100! = 100 * 99!
99! = 99 * 98! and so forth until
1! = 1, and 0! = 1.
You want to know how many trailing 0's are in N! (at least that is how I understand the question).
Think of how many are in 10!
10! = 3628800
so there are two. The reason why is because only 2*5 = a number with a trailing 0 along with 10. So we have a total of 2. (5*4 has a trailing 0 but 4 is a multiple of 2, and besides, we only get to multiply individual numbers once)
It is a good bet, then, that 20! has 4 (it does).
It's now your job to prove (or disprove) that this pattern will hold, and then come up with a way to code it.