How to do a same function on variables with similiar parts in their names?

1 visualización (últimos 30 días)
MNRNI
MNRNI el 10 de Dic. de 2020
Respondida: Steven Lord el 10 de Dic. de 2020
let's say we have variables named as: Time_p1 and Voltage_p1
Time_p2 and Voltage_p2
Time_p3 and Voltage_p3
and so on.
we want to do the following set of comments on each of them.
for Time_p1 and Voltage_p1:
pulse1=[];
pulse1(:,1)= Time_p1;
pulse1(:,2)= Voltage_p1
for Time_p2 and Voltage_p2:
pulse2=[];
pulse2(:,1)= Time_p2;
pulse2(:,2)= Voltage_p2;
and so on.
how can we do this in a simple way?

Respuestas (1)

Steven Lord
Steven Lord el 10 de Dic. de 2020
let's say we have variables named as: Time_p1 and Voltage_p1
Time_p2 and Voltage_p2
Time_p3 and Voltage_p3
and so on.
That's discouraged, but go on ...
we want to do the following set of comments on each of them.
how can we do this in a simple way?
Instead of having multiple variables names that only differ by a number, store them in a numeric array (if they can be combined into a rectangular region) or a cell array (if they can't.)
x = (1:5).';
y = x.^(0:6)
y = 5×7
1 1 1 1 1 1 1 1 2 4 8 16 32 64 1 3 9 27 81 243 729 1 4 16 64 256 1024 4096 1 5 25 125 625 3125 15625
Instead of defining y0 = x.^0, y1 = x.^1, y2 = x.^2, etc. use y(0+1) for x.^0, y(1+1) for x.^1, y(2+1) for x.^2, etc.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by