Change values of a vector (SM to C2)

5 ビュー (過去 30 日間)
Julen Vicente Pipaon
Julen Vicente Pipaon 2021 年 3 月 14 日
編集済み: Harshavardhan 2025 年 5 月 29 日
I don't know why my code doesn't work. My code suppose to de this:
It copys the bits of the vector from left to rigth until find the first 1. Then for the next bits I changes the 1s to 0s anf the 0s to 1s.
It is basically change from SM to C2.
Error: In an assignment A(:) = B, the number of elements in A and B must be the same.
V1(n) = 1-V1
In my code the vector that i want to make is [1 0 1 0]
CODE
V=[1 1 1 0]
V1 = fliplr(V) % 0 1 1 1
n=1;
x = 0;
while n<=length(V1)
if (V1(n) == 0 && x ~= 1) % If there is a 0 it keeps the (0)
V1(n) = 0
elseif (V1(n) == 1 && x ~= 1) % I keeps the fist (1)
V1(n)= 1
x = 1;
elseif x == 1 % When there has been a 1 i change the 0s to 1s and the 1s to 0s
V1(n) = 1-V1
end
n = n+1;
end
V1 = fliplr(V1)
V1 = [V(1) V1(2:4)] % [1 0 1 0]

回答 (1 件)

Harshavardhan
Harshavardhan 2025 年 5 月 29 日
編集済み: Harshavardhan 2025 年 5 月 29 日
The error you are facing is due to the line:
V1(n) = 1-V1
Here, V1 is a vector , so 1-V1 is a vector , but V1(n) is scalar. Assigning a vector to a scalar is not allowed.
The corrected line should be:
V1(n) = 1 - V1(n)
Hope this helps.

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by