Filter löschen
Filter löschen

How to save data in a matrix using looping

3 Ansichten (letzte 30 Tage)
Irshad Qureshi
Irshad Qureshi am 17 Sep. 2022
Bearbeitet: Irshad Qureshi am 18 Sep. 2022
Hello,
I have a problem where I need to read data from several text files, get maximum absolute values from 4 different columns and then save it in the first column of a new matrix followed by repetaetion of same process for next value of Kc and saving the data in next column. The issue is that the resulting matrix is just showing the results of final try and that too in 3rd column of SH with first 2 columns having zeros.
Thanks,
nn = 0;
for Kc = [31528 21018]
Shear = zeros(4,1);
temp1 = readmatrix(strcat('.\Data\sf',num2str(Kc),'.txt'),'delimiter',' '); % Read .txt file
Shear(1,1) = max(abs(temp1(:,2)));
Shear(2,1) = max(abs(temp1(:,8)));
Shear(3,1) = max(abs(temp1(:,14)));
Shear(4,1) = max(abs(temp1(:,20)));
SH = zeros(4,2);
SH(:,nn+1) = Shear(:,1);
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Sep. 2022
you need to increment nn after you finish a column
  4 Kommentare
Walter Roberson
Walter Roberson am 17 Sep. 2022
Bearbeitet: Walter Roberson am 18 Sep. 2022
I recommend that you learn this programming pattern:
Kc_vec = [31528 21018];
num_Kc = numel(Kc_vec);
SH = zeros(4, num_Kc);
for nn = 1 : num_Kc
Kc = Kc_vec(nn);
temp1 = readmatrix(strcat('.\Data\sf',num2str(Kc),'.txt'),'delimiter',' '); % Read .txt file
Shear = zeros(4,1);
Shear(1) = max(abs(temp1(:,2)));
Shear(2) = max(abs(temp1(:,8)));
Shear(3) = max(abs(temp1(:,14)));
Shear(4) = max(abs(temp1(:,20)));
SH(:,nn) = Shear;
end
The key points here are:
  • have an array of values that you want to iterate over
  • figure out the size of the output by examining the size of the array of values
  • pre-allocate the output
  • loop with a variable that exists to act as an index
  • inside the loop, use the index variable to extract the "current" value from the array of values
  • compute using the current value
  • store into the output array indexed by the loop control variable
These steps separate the indexing from the values being indexed over.
There are still times when it is useful to keep a counter of how many results you have produced so-far, and use that counter to index an output array, but if you are growing the output array each time that can get fairly inefficient. You would consider using that kind of arrangement:
  • if you have an indefinite process where the maximum number of iterations is difficult to predict
  • if you only output on some of the iterations (such as if a condition is met)
In the case where you have a definite number of iterations to do but are outputting selectively, then growing the output can add up in cost. Because of that, sometimes it helps to pre-allocate for the maximum possible size, and store values indexed by the loop control variable, but to also keep a vector of values indicating which entries are actually used; and then after all iterations, select out the entries actually used. Or, pre-allocate for the maximum possible size and keep a counter of how many you actually use, and then afterwards, trim off the unused entries.
Irshad Qureshi
Irshad Qureshi am 17 Sep. 2022
Bearbeitet: Irshad Qureshi am 18 Sep. 2022
Its working and thanks for such detailed explanation.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by