Arithmetic Operation on Elements of a Vector satisfying a given condition using Logical Indexing

1 view (last 30 days)
I have a problem statement where i have to change the elements of a vector depending on some condition
the vector comprises of random number between 4 to 12 i.e. 4,5,6,7,8,9,10,11,12
for eg. the given vector is A = [ 5,6,8,9,7,5,4,12,11,9,10]
now i have to add a scalar to the given vector , which can be either a -ive number or a +ive number
and any element of the resultant vector greater than 12 should be wrap around and any if the element is smaller than 4 then also it should be wrapped around.
for eg. if my input scalar is 3 , then the element 11 in input vector after addition operation will become 15 which is greater than 12
and should be wrapped around and changed to 5 , like shift in a circular manner.
in second case taking the input scalar as -4 to be added to the vector A , the element 6 will become 2 and should be wrapped around and changed to 11
I can solve this using for loop and if else statement , but i am interested in solving this via logical indexing.
any help on that will be greately appreciated.
  1 Comment
ANKUR WADHWA
ANKUR WADHWA on 3 May 2020
another point is for eg. i have a vector v where i want to replace all the negative number with 0
v = [ 5 4 3 -2 8 9 10 5 8 3 4 7 12 5]
i can use
v(v<0) = 0 (this works fine and return me a modified vector v of lenght 14)
also i can do the operation v = v-3 and this also return me the modified vector v of lenght 14 again
i am not able to understand why can i combine the above to operation in the same command like
v(v<0) = v-3;
this command gives me an error that
In an assignment A(:) = B, the number of elements in A and B must be the same.

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 3 May 2020
Edited: Ameer Hamza on 3 May 2020
For first question
A = [5,6,8,9,7,5,4,12,11,9,10];
scalar = -4;
lb = 4; % lower bound
ub = 12; % upper bound
A_shift = mod((A-lb)+scalar, ub-lb+1)+lb;
Result
>> A
A =
5 6 8 9 7 5 4 12 11 9 10
>> A_shift
A_shift =
10 11 4 5 12 10 9 8 7 5 6
For second question. Correct syntax is
v(v<0) = v(v<0)-3;
  6 Comments
ANKUR WADHWA
ANKUR WADHWA on 4 May 2020
The code is giving incorrect results for coding and decoding when the scalar is greater than upper bound
or smaler than lower bound , for eg.
when i executed the above code with the below driver , the coded and decoded values are not same
and in some case they are wrong.
wrap = shift('1234', 96)
back = shift(wrap, -96)
Ameer Hamza
Ameer Hamza on 4 May 2020
Also wrap the shifting factor
wrap = shift('1234', 96)
back = shift(wrap, -96)
% the bounds are 32 and 126 in this case
function coded = shift(A, b)
ub = 126;
lb = 32;
b = rem(b, ub-lb);
A_ = A + b;
A_(A_ > ub) = A_(A_ > ub) - ub + lb + 1;
A_(A_ < lb) = A_(A_ < lb) - lb + ub + 1;
coded = char(A_);
end

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!