Filling the empty array

28 vues (au cours des 30 derniers jours)
Sydney Kehoe
Sydney Kehoe le 7 Juin 2021
I am new to matlab and attempting to fill an empty array with data from a for loop using a certain column of data (column f). However, when I try and run the code, it just spit backs out the data from column f and doesn't seem to do anything from the for loop but no errors are shown. Please help.
A = readmatrix('filename');
f = A(:,6);
start_c = [150 278 444 554 612 716];
time_c = [48 42 36 42 42 42];
end_c = [start_c + time_c];
start_dc = [96 214 332 388 496 668];
time_dc = [42 48 42 42 42 36];
end_d = [start_dc + time_dc];
crave = 1;
dcrave = 0;
rest = -1;
empty_array = [];
for ii = 1:length(f)
if f(ii) >= start_c & f(ii) <= end_c
crave;
elseif f(ii) >= start_dc & f(ii) <= end_dc
dcrave;
else f(ii)
rest;
end
empty_array(ii) = f(ii);
end

Réponse acceptée

Star Strider
Star Strider le 7 Juin 2021
The code does not assign ‘crave’, ‘dcrave’ or ‘rest’ to anything.
What do you want the code to do with those values?
In the interim, perhaps something like:
empty_array = zeros(size(f));
for ii = 1:length(f)
if f(ii) >= start_c & f(ii) <= end_c
empty_array(ii) = crave;
elseif f(ii) >= start_dc & f(ii) <= end_dc
empty_array(ii) = dcrave;
else f(ii)
empty_array(ii) = rest;
end
empty_array(ii) = f(ii);
end
will do what you want.
.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by