how assign values from FOR loop to different values

X=[0 0 1 1 0 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1 0 1 0];
B=length(X);
G=4;
Pbits=B/G;
%Separate into parts
for i=0:Pbits-1
x=X(1,i*G+1:(i+1)*G)
if i==i
xx(i+1)=x(i+1)
elseif i==1
x1=x
elseif i==2
x2=x
elseif i==3
x3=x
elseif i==4
x4=x
elseif i==5
x5=x
elseif i==6
x6=x
elseif i==7
x7=x
end
end

4 件のコメント

KSSV
KSSV 2019 年 2 月 19 日
What you are trying to do?
amit barala
amit barala 2019 年 2 月 19 日
i want to separate the X in G groups with different names like x1,x2,x3,etc of length Pbits
Adam
Adam 2019 年 2 月 19 日
Just use them in X with indexing. Are you really going to write a massive if statement to determine which variable you want to use every time you want to access these afterwards?!
You can store an array of indices that tell you where the next part starts in the single variable that you have to start with.
Stephen23
Stephen23 2019 年 2 月 19 日
編集済み: Stephen23 2019 年 2 月 19 日
@amit barala: just use indexing. Indexing is simple, neat, and very efficient.
Using numbered variables is a sign that you are doing something wrong.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2019 年 2 月 19 日

編集済み:

2019 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by