How can I find row matrix indices which contain specific element? [closed] - arrays

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)

Related

how to correct a mistake from two dimensional matrix using matlab programming [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
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.

R - calculating mean of every 3 values in array using a for loop [closed]

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 6 years ago.
Improve this question
Very new to programming - I have an array with dimension 821 and I would like to take the average of every 3 values. Each line represents monthly data and I need the quarterly averages (so every 3 months). I want to do it using for loops for practice but I cannot figure it out.
Explanations in the comments.
# Create some dummy data. I created one with 822 values instead of 821 because 821 is not divisible without remainder by 3.
vec.dummy <- sample(1:100, 822, replace = TRUE)
# Use the values and create a matrix with 3 columns.
mat.dummy <- matrix(vec.dummy, ncol = 3, byrow = TRUE)
# Take the row means of each row.
rowMeans(mat.dummy)
I would avoid using for loops in R if the problem doesn't require it. Keep chugging away and you'll eventually run into a problem that requires it. This one doesn't.
Here's another approach using plyr:
library(plyr)
array <- rnorm(821) #The array you want to average
div <- 822/3 #Need an integer number of dividers
position <- rep(1:div, each=3)
data <- data.frame(
"array"=array,
"position"=position[1:(length(position)-1)]
)
head(data)
array position
1 -0.005807528 1
2 0.997539073 1
3 1.172736615 1
4 0.371252122 2
5 1.291737080 2
6 0.796526756 2
#use plyr to average "array" by common values in other column of DF
data <- ddply(data, c("array"), numcolwise(mean))
head(data)
array position
1 -3.269147 258
2 -3.076415 158
3 -2.962204 242
4 -2.679143 19
5 -2.306075 231
6 -2.283344 223
Having the second column allows you to keep track of the unique values in your vector. It could be the quarters of your data.
As requested by #Cam23 here is a solution in loop form which I don't recommend:
array <- rnorm(821)
div <- 822/3
threes <- c(0, 3*(1:(div-1)))
finalArray <- numeric()
for(i in 1:length(threes)){
calculation <- mean(array[threes[i]+1:3])
finalArray <- c(finalArray, calculation)
}
finalArray[274] <- mean(array[820:821])
The last line is a janky fix due to the fact that 821 isn't nicely divisible by 3.

Algorithm | Given an array[] and k, find number of subsets sum multiple of k [closed]

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.

MATLAB: doubling the occurrences of a value [duplicate]

This question already has an answer here:
MATLAB: Duplicate each element of a vector? [closed]
(1 answer)
Closed 8 years ago.
It's hard to explain so I will show an example of what I would like to do:
x = [1 2 3 4 5]
I would like the outcome to be:
x = [1 1 2 2 3 3 4 4 5 5]
Preferably without the use of a for loop, but either method would be appreciative.
Thanks.
You can also use the Kronecker tensor product (kron function) which is pretty neat:
x = kron(x,ones(1,2))
x =
1 1 2 2 3 3 4 4 5 5
If you want it sorted as you have here, you could do:
y = sort([x x]);
alternatively if the order matters:
y = reshape([x;x],[1,2*length(x)])

Sum array diagonally with Inverted location [closed]

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++]

Resources