Assign Character based on condition

조회 수: 6 (최근 30일)
Turbulence Analysis
Turbulence Analysis 2022년 2월 18일
댓글: Voss 2022년 2월 18일
Hi,
In the below script, if ii = 3, I would like to define X_5 (:,1) = X_5 (:,1)*(2); and for ii =2 it should be Y i.e.
Y_5 (:,1) = Y_5 (:,1)*(2),
may I know how to define this ??
ii = 3;
X_5 (:,1) = X_5 (:,1)*(2);
X_10 (:,1) = X_10 (:,1)*(2);
X_15 (:,1) = X_15 (:,1)*(2);
X_20 (:,1) = X_20 (:,1)*(2);
X_25 (:,1) = X_25 (:,1)*(2);
X_30 (:,1) = X_30 (:,1)*(2);

채택된 답변

Voss
Voss 2022년 2월 18일
if ii == 3
X_5 (:,1) = X_5 (:,1)*(2);
elseif ii == 2
Y_5 (:,1) = Y_5 (:,1)*(2);
end
  댓글 수: 2
Turbulence Analysis
Turbulence Analysis 2022년 2월 18일
Sorry, not like this, in the entire below shown script, X needs to replaced by Y , if I assign ii =2; if it is ii =4, it should be Z.. Something similar to this....
X_5 (:,1) = X_5 (:,1)*(2);
X_10 (:,1) = X_10 (:,1)*(2);
X_15 (:,1) = X_15 (:,1)*(2);
X_20 (:,1) = X_20 (:,1)*(2);
X_25 (:,1) = X_25 (:,1)*(2);
X_30 (:,1) = X_30 (:,1)*(2);
Voss
Voss 2022년 2월 18일
편집: Voss 2022년 2월 18일
if ii == 3
X_5 (:,1) = X_5 (:,1)*(2);
X_10 (:,1) = X_10 (:,1)*(2);
X_15 (:,1) = X_15 (:,1)*(2);
X_20 (:,1) = X_20 (:,1)*(2);
X_25 (:,1) = X_25 (:,1)*(2);
X_30 (:,1) = X_30 (:,1)*(2);
elseif ii == 2
Y_5 (:,1) = Y_5 (:,1)*(2);
Y_10 (:,1) = Y_10 (:,1)*(2);
Y_15 (:,1) = Y_15 (:,1)*(2);
Y_20 (:,1) = Y_20 (:,1)*(2);
Y_25 (:,1) = Y_25 (:,1)*(2);
Y_30 (:,1) = Y_30 (:,1)*(2);
elseif ii == 4
Z_5 (:,1) = Z_5 (:,1)*(2);
Z_10 (:,1) = Z_10 (:,1)*(2);
Z_15 (:,1) = Z_15 (:,1)*(2);
Z_20 (:,1) = Z_20 (:,1)*(2);
Z_25 (:,1) = Z_25 (:,1)*(2);
Z_30 (:,1) = Z_30 (:,1)*(2);
end
If those X_*, Y_*, and Z_* variables are all the same size, you may want to consider combining all of them (and any other similar ones you may also have that I don't know about) into a single variable - maybe it would have 3 or 4 dimensions, depending on if each X_* etc. here is a column vector or a 2D matrix - then it would be easier to do the same operation like this on a specific part of it. You could avoid having to copy/paste and modify your code, preventing potential typographical errors or missing an instance of X that should be Y, for instance.
In this case, assuming each variable is a column vector, then I could combine them all with the _5, _10, etc., going along the second dimension and Y_, X_, Z_, going along the third (not sure why it's not in order of X,Y,Z, but OK):
M = cat(3,[Y_5 Y_10 Y_15 Y_20 Y_25 Y_30],[X_5 X_10 X_15 X_20 X_25 X_30],[Z_5 Z_10 Z_15 Z_20 Z_25 Z_30]);
(of course this step can be avoided if the data is stored all in one such matrix from the start and not split into 15 different variables). Then you can double part of it in place based on the value of ii, like you are doing, like this:
M(:,:,ii-1) = M(:,:,ii-1)*2;
This is much easier to write (and read) and less prone to errors due to replicating pieces of code.

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

추가 답변 (1개)

Turbulence Analysis
Turbulence Analysis 2022년 2월 18일
Actually, X, Y ,Z are not of same size.. I will try the if, else if method as you suggested..
  댓글 수: 1
Voss
Voss 2022년 2월 18일
If all the X_5, X_10, etc. are the same size, and the Y_'s are a different size (but all Y_'s are the same amongst themselves), then you can combine all the X_'s into one, and all the Y_'s into one, etc., and have three variables - one for X, one for Y, one for Z.
Or you can put whatever is the same size together in matrices and put different size matrices into a cell array and do operations on each cell of the cell array using cellfun().
The point is, there are always better options than having to write and maintain highly redundant code, but whatever works for you is what works since you're the one dealing with it!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by