Server Side Programming Articles

Page 1129 of 2109

Find smallest and largest element from square matrix diagonals in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

In this problem, we are given a square matrix of size nXn. Our task is to Find smallest and largest element from square matrix diagonals. We need to find the smallest and largest elements of the primary and secondary diagonals of the matrox.Let’s take an example to understand the problem, Inputmat[][] = {    {3, 4, 7},    {5, 2, 1},    {1, 8, 6} }OutputSmallest element in Primary Diagonal = 2 Largest element in Primary Diagonal = 6 Smallest element in Secondary Diagonal = 1 Largest element in Secondary Diagonal = 7Solution ApproachA simple solution to solve the problem ...

Read More

How to sort each row of an R data frame in increasing order?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To sort each row of an R data frame in increasing order, we can use apply function for sorting the columns and then transpose the output. For example, if we have a data frame called df that contains 5 columns then each row of df can be sorted in increasing order by using the command t(apply(df,1,sort)).Example1Consider the below data frame −x1

Read More

How to minus one column from another in an R matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 6K+ Views

To minus one column from another in an R matrix, we first need to read the matrix as a data frame using as.data.frame then find minus the columns using minus sign and accessing the column of the data frame. To understand how it can be done look at the steps in below examples.ExampleConsider the below data frame −M1

Read More

How to convert negative values in a matrix to 0 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

To convert negative values in a matrix to 0, we can use pmax function. For example, if we have a matrix called M that contains some negative and some positive and zero values then the negative values in M can be converted to 0 by using the command pmax(M,0).ExampleConsider the below data frame −M1

Read More

How to combine a data frame and a named vector if name matches with a column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 955 Views

If we have a data frame that contains a character column and a named vector which has the same names as in the character column of the data frame then we can combine this data frame and the vector by using match function be appropriately defining the names and the character column. Check out the below example to understand how it can be done.ExampleConsider the below data frame df1 and the vector v1 −df1

Read More

How to convert the row values in a matrix to row percentage in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 982 Views

To convert the row values in a matrix to row percentage, we can find the row sums and divide each row value by this sum. For example, if we have a matrix called M then we can convert the row values in M to row percentage by using the commandround((M/rowSums(M))*100,2)ExampleConsider the below matrix −M1

Read More

How to select columns in R without missing values?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

There are two easy methods to select columns of an R data frame without missing values, first one results in a vector and other returns a matrix. For example, if we have a data frame called df then the first method can be used as df[,colSums(is.na(df))==0] and the second method will be used as t(na.omit(t(df))).ExampleConsider the below data frame −df1

Read More

How to check if a vector exists in a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).ExampleConsider the below list −List

Read More

How to split a data frame using row number in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To split a data frame using row number, we can use split function and cumsum function. The split function will split the rows and cumsum function will select the rows. For example, if we have a data frame called df that contains twenty rows then we can split into two data frames at row 11 by using the below command −split(df,cumsum(1:nrow(df)%in%11)).ExampleConsider the below data frame −x1

Read More

How to remove starting and ending zeros in an R vector?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 720 Views

To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −x[min(which(x!=0)):max(which(x!=0))]Examplex1

Read More
Showing 11281–11290 of 21,090 articles
Advertisements