how to make my code simpler ?

조회 수: 2 (최근 30일)
meryem berrada
meryem berrada 2018년 7월 5일
댓글: meryem berrada 2018년 7월 5일
Hello, I have a vector named TB consisting of the mean of variables named T1B, T2B, ..., T44B. I imported these variables from an excel file. Is there a way to simplify the code?
TB=[mean(T1B), mean(T2B), mean(T3B), mean(T4B), mean(T5B),...
mean(T6B), mean(T7B), mean(T8B), mean(T9B), mean(T10B), mean(T11B),...
mean(T12B), mean(T13B), mean(T14B), mean(T15B), mean(T16B), mean(T17B),...
mean(T18B), mean(T19B), mean(T20B), mean(T21B), mean(T22B), mean(T23B),...
mean(T24B), mean(T25B), mean(T26B), mean(T27B), mean(T28B), mean(T29B),...
mean(T30B), mean(T31B), mean(T32B), mean(T33B), mean(T34B), mean(T35B),...
mean(T36B), mean(T37B), mean(T38B), mean(T39B),mean(T40B), mean(T41B),...
mean(T42B), mean(T43B), mean(T44B)];

채택된 답변

Magdy Saleh
Magdy Saleh 2018년 7월 5일
Try something like:
for i=1:num_elems %assuming you know the number of elements and have set it before hand
eval(['TB(i) = mean(T' num2str(i) 'B);'])
end
What this does is that it evaluates the string as matlab code. Using eval is not recommended in general, but might make life easier for you here. (See Using Eval)
  댓글 수: 1
meryem berrada
meryem berrada 2018년 7월 5일
Thank you, it worked perfectly!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by