logo
down
shadow

Comparing elements in each row of a matrix and count the similar values


Comparing elements in each row of a matrix and count the similar values

By : sathish
Date : October 16 2020, 06:10 PM
this will help I have a matrix like this: , I would go for this:
code :
match = sum((line(1, :) == line(2, :)) & (line(1, :) != 0))
ans =
  1  1  0  1  0  0  1
ans =
  1  1  0  0  0  0  1


Share : facebook icon twitter icon
C++---- Comparing the the elements in the 2 arrays and displaying what are the values that are not similar

C++---- Comparing the the elements in the 2 arrays and displaying what are the values that are not similar


By : Artem Grunichev
Date : March 29 2020, 07:55 AM
like below fixes the issue There are a couple of things that you need to understand: - Array will be passed by reference, so you don't need &ans[cnt] - User input could be lower or upper case. It does not matter. What matter is inside your function, how are you going to compare them. You can convert the user input into lower or upper before comparing. - Try not to use global variables if not needed. In here, to keep track of the number of correct answer, you can do it with a local variable, just pass it to the function.
code :
#include <iostream>
#include <string>

using namespace std;

const int NUM_QUESTIONS = 20;
char correctAnsBig[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'D', 'D', 'A' };

char ans[NUM_QUESTIONS];

// check for user input
bool ValidateInput(char input)
{
    input = toupper(input);
    return (input == 'A' || input == 'B' || input == 'C' || input == 'D');
}

void inputAnswers(char input[], int size)
{
    for (int cnt = 0; cnt < NUM_QUESTIONS; cnt++)
    {
        bool valid = false;

        do
        {
            cout << "Enter your answer for #" << (cnt + 1) << " (A, B, C, or D): ";
            cin >> ans[cnt];

            valid = ValidateInput(ans[cnt]);
        } while (!valid);       
    }
}

int checkAnswers()
{
    int count = 0;

    for (int i = 0; i < NUM_QUESTIONS; i++)
    {
        // first, you need to convert all the character in the array to uppercase
        // then you will compare them
        // if they are equal, increase count of correct answer by 1
        // otherwise, do nothing
        if (toupper(ans[i]) == toupper(correctAnsBig[i]))
            count++;
    }
    return count;
}

void rightAns()
{
    cout << "The correct answers are: \n";
    for (int cnt = 0; cnt < NUM_QUESTIONS; cnt++)
    {
        cout << (cnt + 1) << "." << "\t";
        cout << correctAnsBig[cnt] << endl;
    }
}

int main()
{
    int numOfCorrectAnswer = 0;

    inputAnswers(ans, NUM_QUESTIONS);
    rightAns();
    numOfCorrectAnswer = checkAnswers();

    cout << "You got " << numOfCorrectAnswer << " corrects" << endl;
    if (numOfCorrectAnswer == 20)
        cout << "You got it all right" << endl;
    else if (numOfCorrectAnswer > 15)
        cout << "You passed" << endl;
    else
        cout << "You failed" << endl;
}
comparing two arrays elements, in similar indexes with each other

comparing two arrays elements, in similar indexes with each other


By : Z.R Rero
Date : March 29 2020, 07:55 AM
With these it helps This question is the perfect example for the PHP function array_intersect_assoc():
code :
$array1 = array("z","z","E","z","z","E","E","E","E","E");
$array2 = array("a","a","a","z","a","E","E","E","a","E");

// Find the matching pairs (key, value) in the two arrays
$common = array_intersect_assoc($array1, $array2);
// Print them for verification
print_r($common);

// Count them
$match = count($common);
echo($match." common items.\n");
Array
(
    [3] => z
    [5] => E
    [6] => E
    [7] => E
    [9] => E
)
5 common items.
R - count similar values in pair-wise comparing groups in data.frame

R - count similar values in pair-wise comparing groups in data.frame


By : windrises
Date : March 29 2020, 07:55 AM
hop of those help? Maybe you can also use proxy for this problem:
code :
library(proxy)

df <- data.frame(id = c("A", "B", "c"),
                 V1 = c(1, 3, 2),
                 V2 = c(1, 2, 2),
                 V3 = c(3, 1, NA))

myfun <- function(x, y) {
  sum(unique(setdiff(x, NA)) %in% y)
}

pr_DB$set_entry(FUN=myfun, names="myfun", distance=FALSE, loop=TRUE)

similar <- proxy::simil(df[, -1L], method="myfun")

res <- combn(df$id, 2L)
res <- data.frame(id_x=res[2L,], id_y=res[1L,])
res$similar <- as.integer(similar)

print(res)
  id_x id_y similar
1    B    A       2
2    c    A       0
3    c    B       1
df <- data.frame(id = sample(10e2),
                 V1 = sample(1:15, 10e2, replace = TRUE),
                 V2 = sample(2:16, 10e2, replace = TRUE),
                 V3 = sample(3:17, 10e2, replace = TRUE))

system.time({
  similar <- proxy::simil(df[, -1L], method="myfun")

  res <- combn(df$id, 2L)
  res <- data.frame(id_x=res[2L,], id_y=res[1L,])
  res$similar <- as.integer(similar)
})

   user  system elapsed 
   7.84    0.05    7.92
how to iterate through matrix array to count the number of similar elements surrounding a particular element inside the

how to iterate through matrix array to count the number of similar elements surrounding a particular element inside the


By : George Humphreys
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Two issues. The first happens in your for loop. You don't want to count the current index and only want to sample it's neighbors, so you want to throw out cases where x == y == 0. Do this by adding an if statement like if x == y == 0: continue
The second issue is that you're setting the value not incrementing it. This here: result[i][j]= matrix[i+x][j+y] only sets the value to True and doesn't increase it when further neighbors are detected. Instead add like: result[i][j] += matrix[i+x][j+y]
code :
true = True
false = False

matrix = [[true, false, false],
      [false, true, false],
      [false, false, false]]


result = [[0 for x in range(len(matrix[0]))] for y in range(len(matrix))]
for i in range(len(matrix)):
    for j in range(len(matrix[0])):
        for x in [1,0,-1]:
            for y in [1,0,-1]:
                if x == y == 0: continue
                if 0<=i+x<len(matrix) and 0<=j+y<len(matrix[0]):
                    result[i][j] += matrix[i+x][j+y]
for i in result:
    print(i)
[1, 2, 1]
[2, 1, 1]
[1, 1, 1]
Comparing elements of a Fortran matrix to the average of the entire matrix and remembering the index of the number close

Comparing elements of a Fortran matrix to the average of the entire matrix and remembering the index of the number close


By : hojun
Date : September 22 2020, 07:00 AM
I hope this helps . I don't have Fortran on this machine so there may be some syntactic errors in what follows. First, let's find the mean of all the values in mat:
code :
mean = sum(mat)/size(mat)
abs(mat-mean)
minloc(abs(mat-mean))
Related Posts Related Posts :
  • polyfit and polyval funtions
  • Output a matrix size n x m, 1 when the sum of the indices is even, 0 otherwise
  • Saving array in file and then loading it in Octave? Error: struct cannot be indexed with {
  • How does the MATLAB symvar function work?
  • No visible points for plot in for loop
  • Matrix with dynamic dimension in matlab
  • Compare two equal arrays but isequal() function return 0 in Matlab
  • Did the MATLAB R2019a mxArray header change?
  • What is the colon operator doing here?
  • Remove zeros from a matrix
  • How to generate continuous numbers with repetitive increment in matlab
  • Counter in Matlab-mex code does not stop after predetermined iterations
  • Why is my line object being registered as a double in app-designer Matlab?
  • Is the PrincipalAxisLength measurement equivalent to the object diameter in the regionprops3 output?
  • Logical Masking in Matlab - How to (instantly) index concatenated Matlab array?
  • Generate random movement for a point in 3D space
  • MATLAB Data Cursor won't click on every point of plot (R2018b)
  • Matlab angle function to compute phase
  • Quiver plot without arrow stem
  • MATLAB Crashes During the Timer Object Deletion
  • How to run a .mexw64 file on Mac?
  • Polyfit and Polyval in Matlab
  • illegal plot error when trying to use subplot
  • Calculate Interval mean of column in matlab (interval not fixed)
  • Surpress output when calling matlab live script from other live script
  • plotting already saved handle in matlab
  • Compute binary line thickness in normal direction
  • Area measurement of fluorescent powder from finger contacts - batch processing
  • How to perform addition modulo 2 to the power 64
  • How can I use the Cyclic repetition option in the 'From Workspace' Simulink block to include Matlab data in Simulink?
  • Can a MATLAB contour plot look like the one from Mathematica?
  • Show datatips in a logarithmically scaled uiaxes
  • How to superimpose two contour maps onto each other in matlab?
  • Matlab Simscape vs Simulink vs PDE-Toolbox/Ansys/Comsol
  • PowerPoint + Matlab PasteSpecial (Alt+e+s)
  • What is the difference between a uint8 and a single image?
  • How to find peaks in a binary image
  • Extracting certain part of a string using strtok
  • Calculating geodesic distance in MATLAB
  • Why can't I use 'scatter3' here?
  • How to cut out parts of the y-axis for certain x-values in Matlab plots
  • I want to write 0 & 1 in binary file in form of bits
  • Does MATLAB use "0" to represent infinitely-many solutions?
  • Program to find prime numbers in MATLAB
  • How to calculate cosine similarity between two frequency vectors in MATLAB?
  • How to translate ABM Model from MATLAB to NetLogo?
  • webread giving different results than 'View Page Source' of webpage
  • Portable declaration of REAL variables in mex gateway for Fortran
  • How to fix the difference in precision between double data type and uint64
  • CTRL + C is not working while ROS video callback is working
  • How to simulate random point following multivariate t distribution?
  • Parallellising the factorisation of triangle numbers
  • How can I do side-by-side simulation of Simulink files by using for-loop in MATLAB GUI or is there another way to do it?
  • How to compare the values of an one-dimensional matrix with each other?
  • Conflict between 'ShowText' and 'LineStyle' in the contourf function in MATLAB R2019a?
  • How to load tables and save tables from different .mat files and create new table dimension for each file
  • How can I calculate int(exp(sin(x)),x) in MATLAB
  • MATLAB improving performance of sum operator
  • How to obtain an equation for a line fitted to data
  • Matlab - how does contour plot generating levels automatically?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk