Filling the empty array

26 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2021-6-7
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.
.

更多回答(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