Filling the empty array

조회 수: 69 (최근 30일)
Sydney Kehoe
Sydney Kehoe 2021년 6월 7일
답변: Star Strider 2021년 6월 7일
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