How to access array element sequentially in for loop?

Hello,
I have 5 dataset:
data1=rand(1,1000)
data2=rand(1,1000)
data3=rand(1,1000)
data4=rand(1,1000)
data5=rand(1,1000)
To run my simulation, I am running a for loops for i=1:200 (running for loops for 200 times) where when i=1, I have to select the 3 data from data1 starting from index 1 to 3, then from data2 I have to select 2 data and similar way way from all the arrays.
I have to select the values sequentially and a non-repetitive way. That means, when i=2, I have to select data from data1 from index 4 to 6.
Then I will use that data in for calculation. Like, result would be:
result = data1(index1) + data2(index1);
result2=data1(index2) + data2(index2);
at the end of the simulation, I will have result vaules that contains 200 data.
Thank you so much for your time and considerations.

3 Commenti

The way you use your data in your explanation does not appear to be consistent with you selecting 3 data from data1 but only 2 data from data2. Your line
result = data1(index1) + data2(index1);
can only work if you select the same number of elements from data1 and data2.
Thank you so much for your reply.
Actually I wil use the data1 with others data. I will select data from each dataset.
My whole pocess is for i=1,
Result1(index1)=data1(index1)+data1(2)
Result2(index1)= data2(index1)+data2(index2)
result3(index1)=data1(index3)+data2(index1)+data3(index1)
result4(index1)=data1(index4)+data3(index2)
And i want to continue this process for i=200,where array index should be in sequential way.
like following way:
Result1(index2)=data1(index5)+data1(index6)
Result2(index2)=data2(index3)+data2(index4)
result3(index2)=data1(index7)+data2(index3)+data3(index3)
result4(index2)=data1(index8)+data3(index4)
Thank you for your time and considerations.

Accedi per commentare.

Risposte (2)

Rather than creating numbered variables (which is generally discouraged) consider making a matrix or array containing all your data.
data = rand(5, 1000);
q = data(3, 42) % what you would call data3(42) in your original code
I have no idea what pattern you're using to try to extract elements from your data so I can't give any advice about that part. If you describe that pattern in more detail we may be able to help you determine the most efficient way to extract the data.

5 Commenti

Thank you so much. I will make it clear.
I am trying following code.
Here I am using index values for data1 from 1 to 4 but in case of data2, I am using index value of 1 to 2. But when I am increamenting my for loop at 4, it skips the 2 index values of data2 which should be start from index 3, not from index 5.
data1=rand(1,1000)
data2=rand(1,1000)
data3=rand(1,1000)
len=200;
trigger=2
for i_value= 1:4:len
delay1=data1(i_value)
delay2=data1(i_value+1)
propagation_delay_1=(delay1+delay2)/2
delay3=data2(i_value)
delay4=data2(i_value+1)
propagation_delay_2=(delay3+delay4)/2
RTT_1=data2(i_value)+data3(1)+data1(i_value+3)
final_delay= RTT_1-propagation_delay_1-propagation_delay_2
triggering_time=trigger- final_delay-propagation_delay_1
delivery_time(i_value)=triggering_time+data1(i_value+4)+data3(i_value+3)
end
reshape your arrays into number_used_per_cycle by number_of_cycles . Then instead of looping over ivalue being the index into data1, loop over cycle numbers. You can access data1m(1, cycle), data1m(2,cycle), data2m(1,cycle) and so on.
Can you please kindly give an example
Tania, you are incrementing i_value by 4 at a time, but you use up to data1(i_value+4) . On the first iteration you would use data1(1), data1(2), data1(4), data1(5) (but not data1(3)) . On the second iteration you would use data1(5), data1(6), data1(8), data1(9) (but not data1(7)) . Is it correct that you want to use data1(5) in the first iteration and also use it in the second iteration?
Is it correct that every iteration you want to use data3(1), as well as some other values of data3 ?

Accedi per commentare.

I have used 2 more variable to keep track of data2 and data3 index.
Running the following loop we will be able to select data from these data vectors. The data are stored in variable from delay1 to delay6
Hope this helps.
if true
data1=rand(1,1000);
data2=rand(1,1000);
data3=rand(1,1000);
len=200;
trigger=2;
count1 =1;
count2 =1;
for i_value= 1:3:len
% selecting 3 data from data1
delay1=data1(i_value);
delay2=data1(i_value+1);
delay3=data1(i_value+2);
% selecting 2 data from data2
delay4=data2(count1)
delay5=data2(count1+1)
count1 = count1+2;
% selecting 2 data from data2
delay6=data3(count2)
count2 = count2+1;
end
% code
end

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 4 Lug 2020

Commentato:

il 17 Lug 2020

Community Treasure Hunt

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

Start Hunting!

Translated by