Borrar filtros
Borrar filtros

How can I generate three random matrices from 20 percent of the rows of a 2000 by 80 matrix, using for loop?

1 visualización (últimos 30 días)
Let's X=[2000 by 80]. Now I want to get X1, X2 and X3 randomly from X. Each matrix is 20 percent of the X.
Please help me out.
Thank you

Respuesta aceptada

DGM
DGM el 24 de Jun. de 2021
I don't see why a loop is necessary. Is it?
A = rand(2000,80); % test array
s = size(A,1);
nrows = round(s*0.2);
X1 = A(randperm(s,nrows),:);
X2 = A(randperm(s,nrows),:);
X3 = A(randperm(s,nrows),:);

Más respuestas (1)

Chunru
Chunru el 24 de Jun. de 2021
a = magic(20);
for i = 1:3
idx = randperm(20, 4); % 4 is 20% of 20; for your case 2000*20%
b = a(idx, :)
end
b = 4×20
160 242 243 157 156 246 247 153 152 250 251 149 148 254 255 145 144 258 259 141 20 382 383 17 16 386 387 13 12 390 391 9 8 394 395 5 4 398 399 1 21 379 378 24 25 375 374 28 29 371 370 32 33 367 366 36 37 363 362 40 341 59 58 344 345 55 54 348 349 51 50 352 353 47 46 356 357 43 42 360
b = 4×20
281 119 118 284 285 115 114 288 289 111 110 292 293 107 106 296 297 103 102 300 20 382 383 17 16 386 387 13 12 390 391 9 8 394 395 5 4 398 399 1 240 162 163 237 236 166 167 233 232 170 171 229 228 174 175 225 224 178 179 221 341 59 58 344 345 55 54 348 349 51 50 352 353 47 46 356 357 43 42 360
b = 4×20
181 219 218 184 185 215 214 188 189 211 210 192 193 207 206 196 197 203 202 200 341 59 58 344 345 55 54 348 349 51 50 352 353 47 46 356 357 43 42 360 101 299 298 104 105 295 294 108 109 291 290 112 113 287 286 116 117 283 282 120 160 242 243 157 156 246 247 153 152 250 251 149 148 254 255 145 144 258 259 141

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by