# Problem with matrices

Hello, I have a problem with arrays. R generates almost identical C matrices from a certain point when I try to compare the elements of B and D matrices. The code seems to work correctly. Could this be a problem with numbers that are too large? Here is the code

``````#Matrix B
k_1<-6
n_1<-175
B<-matrix(rep(0,n_1*k_1), nrow  = k_1)
for (k in 1:k_1) {
for (n in 1:n_1) {
if (n<=(max(2,k-1)+1)) {
B[k,n]<-1
} else {
B[k,n]<-B[k,n-3]+B[k,n-k]
}
}
}
#Matrix D
l<-list()
s<-1
D<-matrix(rep(0,25*k_1), nrow = k_1)
for (j in 1:6) {
for (i in 1:6) {
for (k in 1:k_1) {
for (n in 1:25) {
if (n==1){
D[k,n]<-B[k,i]
} else {
D[k,n]<-B[k,(1+j)*n-(2*j+1)+i+j]+D[k,n-1]
}
}
}
l[[s]]<-D
s<-s+1
}
}
# Matrix C
C<-matrix(rep(0,25*k_1), nrow = k_1)
l1<-list()
for (i in 1:(length(l))) {
D1<-l[[i]]
for (k in 1:k_1) {
for (n in 1:25) {
for (t in 1:n_1) {
if (n<=t) {
if (D1[k,n]==B[k,t]-1){
C[k,n]<-t
}
}
}
}
}
l1[[i]]<-C
}
``````

I dont think its about size, its about how close should two floating point numbers be to be considered the same/equivalent.
you can use a convenience function like dplyr::near() to evaluate two values to a given tolerance.

``````> sqrt(2) ^ 2 == 2
 FALSE
> near(sqrt(2) ^ 2, 2)
 TRUE``````

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.