deriving new variable from existing column vector using for-loop

조회 수: 5 (최근 30일)
Hello Matlab community!
I am a newbie matlab user and trying to be comfortable in using for-loops to derive a new variable.
In my example code below. I designated variable 'c' as my column vector of zeros and ones. I wish to run a for loop for that variable and create a new variable 'data_new' with the following conditions:
** if the value of c is 1 then assign a random variable between 5 to 29 else 0.
**it seems to create those zeros but failed to assign a random variable for the if condition that I stated. here's the error message that I encountered: Unable to perform assignment because the left and right sides have a different number of elements.
a=ones(1,4);
b=zeros(1,4);
c=[a b]'; %column vector 8x1
existing_data=c
for i=1:length(c)
if (existing_data(i)==0)
data_new(i)=randi([5,29],8,1)' %% i wish to generate the same size column vector where the ones == randomly assigned data bet 5-29
else
data_new(i)=0
end
end
Thank you!

채택된 답변

Harry Laing
Harry Laing 2020년 12월 9일
Simple error. Your data_new(i)=randi([5,29],8,1)' is the problem. Try putting the line randi([5,29],8,1) ' into the command window and see what happens. You're wanting to assign a single value into data_new but your code tries to assign an entire vector, hence the error.
Try this line instead (change the 8 to a 1):
data_new(i)=randi([5,29],1,1);
As side note, MATLAB will provide you with a warning saying that data_new will change size each iteration. Whilst it may not make much difference with such a small vector, with larger datasets this can cause issues with making the code run much slower. Wherever possible, it is good practice to pre-initiaalise the vector before the loop. For numeric matrices I personally like to create an 'empty' matrix of NaN values before loops like yours. For example, I would write the following into your script before the for loop, but after you create the existing_data variable:
data_new = NaN( size(existing_data) );
  댓글 수: 3
Harry Laing
Harry Laing 2020년 12월 10일
randi is only generating 1 value here, between 5 and 29 (the 1,1 part says to create a vector of size 1x1). Each time the for loop passes, a new number will be generated regardless of the previous. The value chosen is already saved, in your data_new variable.
Do you mean you want the random numbers generated to be the same every time you run your code? By definition, that's not random. But to do this I would generate a long list of random numbers in a vector, save the variable, then load it every time you run your code so the 'random' numbers are the same each time.
Arturo Jr. Ongkeko
Arturo Jr. Ongkeko 2020년 12월 10일
Hi, Harry. Appreciate your reply. I saved my workspace where I created the random values and created another script for data analysis.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by