필터 지우기
필터 지우기

Specify columns inside a for loop

조회 수: 2 (최근 30일)
Andrew Alkiviades
Andrew Alkiviades 2011년 7월 13일
Hi
I would like to know if the code I attached will do the following: I want the value in the for loop to multiply by the price. But more specifically, I want this value to multiply by 10 if it is in the columns 1-8 and for the rest of the columns multiply by 15.
Thanks
for j=1:365
for i=1:25
if data(1,i)>=1 && data(1,i)<=8
price=10;
else
price=15;
end

채택된 답변

Nathan Greco
Nathan Greco 2011년 7월 13일
You are only checking of the data within any given column is between 1 and 8, which isn't what you describe to be wanting.
Just compare i's:
if i >= 1 && i <= 8
%...
else
%...
end
I represents the columns you are iterating over, so you would want to test versus i.

추가 답변 (1개)

Chirag Gupta
Chirag Gupta 2011년 7월 13일
You might not need to use a for loop
% rand data
data = rand(365,25);
% colums 1-8
data(:,1:8) = data(:,1:8)*10;
% colums 9 -25
data(:,9:25) = data(:,9:25)*15;
  댓글 수: 1
Nathan Greco
Nathan Greco 2011년 7월 13일
True, but that's not what he asked.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by