Filling the empty array

28 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by