Why does my Matlab code not work correctly?

user5340565

My code

    b(abs(b(1:3:length(b))) > 0.75) = 0.75

What it's supposed to do:

    b1 = b(1:3:end);
    b1(abs(b1)>0.75) = 0.75;
    b(1:3:end) = b1;

How are these two not the same?

Steve

The indexing part b(1:3:end) returns a short vector of zeros and ones, so will only change the i-th entry of b (for i in the first third-ish of b) to 0.75 if the absolute value of the 3*i + 1-th entry is greater than 0.75.

For example:

b = [-0.684; 0.941; 0.914; -0.029; 0.6; -0.716; -0.156; 0.831; 0.584; 0.919];
b_index = abs(b(1:3:length(b)))>0.75

would return

b_index =

     0
     0
     0
     1

and b(b_index) = 0.75 would change the 4th entry of b to 0.75.

One way of doing this as a one-liner is

b(1:3:end) = b(1:3:end).*(abs(b(1:3:end))<=0.75)) + 0.75*((b(1:3:end)>0.75));

but I think the three-liner is a bit clearer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my array editing not work correctly?

From Dev

Why does my Matlab Random Shuffle not work?

From Dev

why my angular js code does not work

From Dev

Why does inserting a printf statement make my function work correctly?

From Dev

Why the insert method of list does not work correctly in my recursion?

From Dev

Why does inserting a printf statement make my function work correctly?

From Dev

Why does my loop only work correctly through the first iteration?

From Dev

My matlab code doesnt work but i dont know why?

From Dev

Why does my code not work on a device, but work on the emulator?

From Dev

Why doesnt this code work correctly

From Dev

why count(*) does not work correctly?

From Dev

why count(*) does not work correctly?

From Dev

Why does this code not function correctly?

From Dev

Why is my resultant signal not appearing correctly? MATLAB

From Dev

Warning: implicit declaration of function — why does my code work anyway?

From Dev

Arduino. Why does my code work with a delay

From Dev

Why does my code only work properly one time?

From Dev

Why does this code work on my test server but not on jsfiddle?

From Dev

Why does my PHP code doesn't work anymore for no reason?

From Dev

why does my jquery code not work on mobile browser

From Dev

Why my Java multithreading code does not work properly?

From Dev

Why does my Sobel edge detection code not work?

From Dev

Why does my search php code not work properly?

From Dev

Why does my code work only on Virtual Server?

From Dev

Apply CSS upon query string - My code does not work, but why?

From Dev

Why does this MATLAB 'if' statement not work?

From Dev

My 'decrypt' code doesn't work correctly

From Dev

AWK does not work correctly in my system

From Dev

Why does my Kivy BoxLayout not work at the top of my code but it does at the bottom?

Related Related

  1. 1

    Why does my array editing not work correctly?

  2. 2

    Why does my Matlab Random Shuffle not work?

  3. 3

    why my angular js code does not work

  4. 4

    Why does inserting a printf statement make my function work correctly?

  5. 5

    Why the insert method of list does not work correctly in my recursion?

  6. 6

    Why does inserting a printf statement make my function work correctly?

  7. 7

    Why does my loop only work correctly through the first iteration?

  8. 8

    My matlab code doesnt work but i dont know why?

  9. 9

    Why does my code not work on a device, but work on the emulator?

  10. 10

    Why doesnt this code work correctly

  11. 11

    why count(*) does not work correctly?

  12. 12

    why count(*) does not work correctly?

  13. 13

    Why does this code not function correctly?

  14. 14

    Why is my resultant signal not appearing correctly? MATLAB

  15. 15

    Warning: implicit declaration of function — why does my code work anyway?

  16. 16

    Arduino. Why does my code work with a delay

  17. 17

    Why does my code only work properly one time?

  18. 18

    Why does this code work on my test server but not on jsfiddle?

  19. 19

    Why does my PHP code doesn't work anymore for no reason?

  20. 20

    why does my jquery code not work on mobile browser

  21. 21

    Why my Java multithreading code does not work properly?

  22. 22

    Why does my Sobel edge detection code not work?

  23. 23

    Why does my search php code not work properly?

  24. 24

    Why does my code work only on Virtual Server?

  25. 25

    Apply CSS upon query string - My code does not work, but why?

  26. 26

    Why does this MATLAB 'if' statement not work?

  27. 27

    My 'decrypt' code doesn't work correctly

  28. 28

    AWK does not work correctly in my system

  29. 29

    Why does my Kivy BoxLayout not work at the top of my code but it does at the bottom?

HotTag

Archive