필터 지우기
필터 지우기

creating a field in a structure

조회 수: 2 (최근 30일)
mark
mark 2014년 1월 21일
편집: Image Analyst 2014년 1월 22일
I have a structure of 10 cars [1 x 10] in that structure i have 3 fields called Model, Year, Price I want to add another field to all 10 cars called mileage ex.
Car(1)
-Model
- year
- Price
* Mileage*
Car(2)
-Model
- year
- Price
* Mileage*
And store a scalar value into mileage
How would I do that?

답변 (1개)

David Sanchez
David Sanchez 2014년 1월 21일
N_cars = 10;
for k=1:N_cars
car(k).Mileage = mileage_array(k); % insert your value here
end
  댓글 수: 2
mark
mark 2014년 1월 22일
How would you do this without looping?
Image Analyst
Image Analyst 2014년 1월 22일
편집: Image Analyst 2014년 1월 22일
Don't you think it would be:
car(1).Mileage = mileage_array(1);
car(2).Mileage = mileage_array(2);
car(3).Mileage = mileage_array(3);
car(4).Mileage = mileage_array(4);
car(5).Mileage = mileage_array(5);
car(6).Mileage = mileage_array(6);
car(7).Mileage = mileage_array(7);
car(8).Mileage = mileage_array(8);
car(9).Mileage = mileage_array(9);
car(10).Mileage = mileage_array(10);
Don't be one of those with an unnatural, unjustified phobia against looping. You could loop a million times in a fraction of a second. Now if you have an array of tens of millions of cars, then it could start to take a while. But don't sweat 1 microsecond over 2 microseconds - just do what is easiest to understand and most intuitive.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by