-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorfirst.cpp
More file actions
174 lines (117 loc) · 4.25 KB
/
colorfirst.cpp
File metadata and controls
174 lines (117 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <Rcpp.h>
#include <random>
#include <cmath>
using namespace Rcpp;
// [[Rcpp::export]]
double euclideanDistance(NumericVector v1, NumericVector v2) {
double distance = 0.0;
int size = v1.size();
for (int i = 0; i < size; i++) {
double diff = v1[i] - v2[i];
distance += diff * diff;
}
return distance;
}
// [[Rcpp::export]]
NumericMatrix kmeansppC(NumericMatrix indexcombos, int k) {
int dimMat = indexcombos.nrow();
// Vector of 1:(no_obs)
NumericVector indices(dimMat);
for (int n = 0; n < dimMat; n++) {
indices[n] = n;
}
NumericMatrix clusterCenters(k, 3);
//Initial Cluster Center
int randomIndex = Rcpp::sample(indices, 1, true)[0];
for (int l = 0; l < 3; l++) {
clusterCenters(0, l) = indexcombos(randomIndex, l);
}
Rcout << "|--->";
for (int indx = 1; indx < k; indx++) {
NumericVector distance(dimMat); //Vector to store distances
for (int obsnum = 0; obsnum < dimMat; obsnum++) {
double d = 4.0,d1 = 0.0; // Distance cannot be more than 4
for (int clustindex = 0; clustindex < indx; clustindex++) {
d1 = euclideanDistance(indexcombos(obsnum, _), clusterCenters(clustindex, _));
if (d1 < d) { //If current distance less than prev. distance
d = d1;
}
}
distance[obsnum] = d; //store the minimum centroid distance
}
//Probability Vector proportional to D(x)^2
NumericVector p = distance / sum(distance);
//Selecting New Cluster
int selectedIdx = Rcpp::sample(indices, 1, true, p)[0];
for (int l = 0; l < 3; l++) {
clusterCenters(indx, l) = indexcombos(selectedIdx, l);
}
Rcout << "|--->";
}
return clusterCenters;
}
//[[Rcpp::export]]
NumericVector FindClusterIndexC(NumericMatrix indexcombos,NumericMatrix clusterCenters){
int dimMat = indexcombos.nrow();
NumericVector clusterIndex(dimMat);
for(int i = 0;i < dimMat;i++){
double d = 4,d1 = 0; //Distance cannot be more than 4
for(int j = 0; j < clusterCenters.nrow();j++){
d1 = euclideanDistance(indexcombos(i,_),clusterCenters(j,_));
if(d1 < d){
clusterIndex[i] = (j+1);
d = d1;
}
}
}
return(clusterIndex);
}
//[[Rcpp::export]]
List kmeansImageC(NumericMatrix indexcombos,NumericMatrix clusterCenters){
int dimMat = indexcombos.nrow();
NumericVector clusterIndex1 = FindClusterIndexC(indexcombos,clusterCenters);
NumericMatrix clusterCentersNew(clusterCenters.nrow(),3);
int adjustment = 1;
while (adjustment != 0) {
Rcout << "|---->";
adjustment = 0;
for(int clustindx = 0;clustindx < clusterCenters.nrow();clustindx++){
double Rcoord = 0,Gcoord = 0,Bcoord = 0;
int count = 0;
for(int obsnum = 0;obsnum < dimMat;obsnum++){
if(clusterIndex1[obsnum] == (clustindx + 1)){
Rcoord += indexcombos(obsnum,0);
Gcoord += indexcombos(obsnum,1);
Bcoord += indexcombos(obsnum,2);
count += 1;
}
}
clusterCentersNew(clustindx,0) = Rcoord/count;
clusterCentersNew(clustindx,1) = Gcoord/count;
clusterCentersNew(clustindx,2) = Bcoord/count;
}
NumericVector clusterIndex2 = FindClusterIndexC(indexcombos,clusterCentersNew);
for(int i = 0;i < clusterIndex1.length();i++){
if(clusterIndex1[i] != clusterIndex2[i]){
adjustment = 1;
break;
}
}
clusterIndex1 = clusterIndex2;
}
List returnlist = List::create(Named("clusterCenters") = clusterCentersNew,Named("clusterIndex") = clusterIndex1,Named("indexcombos") = indexcombos);
return(returnlist);
}
//[[Rcpp::export]]
NumericMatrix ReplaceMat(NumericMatrix indexcombos,NumericMatrix colors,NumericVector clusterIndex){
for(int i = 0;i < indexcombos.nrow();i++){
for(int j = 0;j < colors.nrow();j++){
if((j+1) == clusterIndex[i]){
for(int k = 0;k < 3;k++){
indexcombos(i,k) = colors(j,k);
}
}
}
}
return indexcombos;
}