简体   繁体   中英

How can I find the index of the last greater than or equals value in one array, for all values in the first array? MATLAB

I have 2 vectors. A is a list of dates, every day for 3 years. B is a list of (option expiration) dates, which occur once a month.

A is therefore much larger than B. I want a vector the length of A, which for each entry in A finds the index of the last >= value in B.

In this way I can get an expiration date (from B) for every date in my vector A.

I would like to accomplish this without using a loop.

Thanks for the help.

Edit: A, B below (abbreviated)

B = 735126 735154 735189 735217 735245 735280 735308 735336 735364 735399 735427 735462 735490 735518 735553 735581 735609 735644

A = 735126 735127 735128 735129 735130 735131 735132 735133 735134 735135 735136 735137 735138 735139 735140 735141 735142 735143 735144 735145 735146 735147 735148 735149 735150 735151 735152 735153 735154 735155 735156 735157 735158 735159 735160 735161 735162 735163 735164 735165 735166 735167 735168 735169 735170 735171 735172 735173 735174 735175 735176 735177 735178 735179 735180 735181 735182 735183 735184 735185 735186 735187 735188 735189 735190 735191 735192 735193 735194 735195 735196 735197 735198 735199 735200 735201 735202 735203 735204 735205 735206 735207 735208 735209 735210 735211 735212 735213 735214 735215 735216 735217 735218 735219 735220 735221 735222 735223 735224 735225 735226 735227 735228 735229 735230 735231 735232 735233 735234 735235 735236 735237 735238 735239 735240 735241 735242 735243 735244 735245 735246 735247 735248 735249 735250 735251 735252 735253 735254 735255 735256 735257 735258 735259 735260 735261 735262 735263 735264 735265 735266 735267 73 5268 735269 735270 735271 735272 735273 735274 735275 735276 735277 735278 735279 735280 735281 735282 735283 735284 735285 735286 735287 735288 735289 735290 735291 735292 735293 735294 735295 735296 735297 735298 735299 735300 735301 735302 735303 735304 735305 735306 735307 735308 735309 735310 735311 735312 735313 735314 735315 735316 735317 735318 735319 735320 735321 735322 735323 735324 735325 735326 735327 735328 735329 735330 735331 735332 735333 735334 735335 735336 735337 735338 735339 735340 735341 735342 735343 735344 735345 735346 735347 735348 735349 735350 735351 735352 735353 735354 735355 735356 735357 735358 735359 735360 735361 735362 735363 735364 735365 735366 735367 735368 735369 735370 735371 735372 735373 735374 735375 735376 735377 735378 735379 735380 735381 735382 735383 735384 735385 735386 735387 735388 735389 735390 735391 735392 735393 735394 735395 735396 735397 735398 735399 735400 735401 735402 735403 735404 735405 735406 735407 735408 735409 735410 7 35411 735412 735413 735414 735415 735416 735417 735418 735419 735420 735421 735422 735423 735424 735425 735426 735427 735428 735429 735430 735431 735432 735433 735434 735435 735436 735437 735438 735439 735440 735441 735442 735443 735444 735445 735446 735447 735448 735449 735450 735451 735452 735453 735454 735455 735456 735457 735458 735459 735460 735461 735462 735463 735464 735465 735466 735467 735468 735469 735470 735471 735472 735473 735474 735475 735476 735477 735478 735479 735480 735481 735482 735483 735484 735485 735486 735487 735488 735489 735490 735491 735492 735493 735494 735495 735496 735497 735498 735499 735500 735501 735502 735503 735504 735505 735506 735507 735508 735509 735510 735511 735512 735513 735514 735515 735516 735517 735518 735519 735520 735521 735522 735523 735524 735525 735526 735527 735528 735529 735530 735531 735532 735533 735534 735535 735536 735537 735538 735539 735540 735541 735542 735543 735544 735545 735546 735547 735548 735549 735550 735551 735552 735553 735554 735555 735556 735557 735558 735559 735560 735561 735562 735563 735564 735565 735566 735567 735568 735569 735570 735571 735572 735573 735574 735575 735576 735577 735578 735579 735580 735581 735582 735583 735584 735585 735586 735587 735588 735589 735590 735591 735592 735593 735594 735595 735596 735597 735598 735599 735600 735601 735602 735603 735604 735605 735606 735607 735608 735609 735610 735611 735612

Answer (using Excel) = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 17 17 17 17

One approach -

[~,idx] = max(cumsum(bsxfun(@ge,A(:),B(:).'),2),[],2)

If you would like to remove cumsum -

[~,idx] = max(bsxfun(@ge,A(:),fliplr(B(:).')),[],2);
idx = numel(B) - idx + 1

Sample run -

>> A,B
A =
     1     6     7     9     0     2     4     9     5
B =
     0     2     8     4     5
>> idx
idx =
     1
     5
     5
     5
     1
     2
     4
     5
     5

I'm assuming you want the last value of B such that all values up to that one satisfy the condition .

[~, result] = max(bsxfun(@gt, B(:), A(:).'), [], 1);
result = result-1;
result(~result) = numel(B);

Example:

>> A = [1 9 6 8 5];
>> B = [1 3 7 2];
>> [~, result] = max(bsxfun(@gt, B(:), A(:).'), [], 1);
>> result = result-1;
>> result(~result) = numel(B);

gives

A =
     1     9     6     8     5

B =
     1     3     7     2

result =
     1     4     2     4     2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM