How to save arrays from a loop with variable names using iterator and how to perform element-by-element operations on arrays and save

3 visualizzazioni (ultimi 30 giorni)
I have a collection of data output that is really composed of N = integer # of interleaved datasets. I have a separate calculation that tells me the value of N. I'm trying to decompose the data into mini-datasets and perform a simple analysis on them.
The raw dataset is:
data = [185.310036632308 185.512007636984 186.306844236548 186.528138471704 187.303737370698 187.545236753824 188.299820779158 188.562095668486 189.295658588882 189.578734814646 190.291895662591 190.594513645617 191.286641304508 191.611037957123 192.282044124896 192.627257895713 193.276914735870 193.643536979820 194.271905231774 194.659684369798 195.266728214912 195.676026040601 196.261394685284 196.691609922683].'
The format of the data will basically be overlapping lists. So, if N=2 then the mini-datasets would be of the form data1=(data[i] data[i+2] data[i+4] ...) and data2 = (data[i+1] data[i+3] data [i+5] ...).
Q1: How can I incorporate the iterator i into the variable name?
I figured out how to partition the datasets for arbitrary values of N with the following for loop. In the example below N=2 but it could be any integer. The code below works but it overwrites the variable name 'datai' each time. How can I incorporate the iterator i into the variable name so this doesn't happen and I can access the variables later?
N =2;
for i= 1:N
datai= data(i:N:end)
end
Q2: How do I perform element-by-element operations on an array to create a new array?
I'd like to take this a step further and calculate properties on an element-by-element basis and form a new array. As a simple example, suppose I want to modify matlabs diff operation to calculate the difference across a point instead of just the difference between successive pairs of points. So instead of differences1=(data1[k]-data1[k+1] data1[k+1]-data[k+2 ...) for k=1:end of data1, I want to calculate alt_differences1=( (data1[k-1]-data1[k+1])/2 (data1[k]-data1[k+2])/2 ...) for k=2:end of data1. How can I write a simple element by element operation like this?

Risposta accettata

Stephen23
Stephen23 il 23 Feb 2021
Modificato: Stephen23 il 23 Feb 2021
"How can I incorporate the iterator i into the variable name?"
Don't do that. The simpler and much more efficient approach is to use indexing:
N = 2;
C = cell(1,N);
for k = 1:N
C{k} = data(k:N:end);
end
"How do I perform element-by-element operations on an array to create a new array?"
Where V is your vector (which could be stored in a cell array):
(V(1:end-2)-V(3:end))/2
  2 Commenti
Steven Lord
Steven Lord il 23 Feb 2021
x = (1:10).^2
x = 1×10
1 4 9 16 25 36 49 64 81 100
y = [NaN NaN x(3:end)-x(1:end-2)]
y = 1×10
NaN NaN 8 12 16 20 24 28 32 36
8, which is y(3), is 9 minus 1 or x(3) minus x(1). 32, which is y(9), is 81 minus 49 or x(9) minus x(7). I added the NaN values at the start of y so the elements of y would line up visually with the later element of x used in the calculation of that element of y.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and 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!

Translated by