Assign Character based on condition
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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);
0 Commenti
Risposta accettata
Voss
il 18 Feb 2022
if ii == 3
X_5 (:,1) = X_5 (:,1)*(2);
elseif ii == 2
Y_5 (:,1) = Y_5 (:,1)*(2);
end
2 Commenti
Voss
il 18 Feb 2022
Modificato: Voss
il 18 Feb 2022
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.
For reference: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
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.
Più risposte (1)
Turbulence Analysis
il 18 Feb 2022
1 Commento
Voss
il 18 Feb 2022
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!
Vedere anche
Categorie
Scopri di più su Cell Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!