Storing values from inside the third loop to a variable outside the loops

Hi all,
Is it possible to store values from the inside of a third for loop into a variable which is outside the loops? For instance I have this code:
a = 2:1:5;
b = [1 1.2 2 2.3 3 3.3 4 5];
final_data = [];
for ii=1:length(a)
c = rand(3,8);
for jj=1:length(b)
c_data = c(:,jj);
if (a(ii) > b(jj) )
for xx = 1:length(c_data)
final_data(xx,ii) = c_data(xx);
end
end
end
end
and I want the values in 'c_data' to be stored inside 'final_data' as rows (the columns are determined by the value of ' ii ' ) for every 'jj' value. But it so happens that the values get over-written for every ' jj ' loop iteration. Or in other words, I was looking for a way to vertically append c_data to final_data for every jj loop iteration without being overwritten.
Thanks

4 Comments

Could you give an example, with smaller input data dimensions, what the final result is supposed to look like?
Sure. Let us suppose:
a = a = 1:1:3;
b = 0.1:0.1:0.5;
c = [100 200 300 400 500; 101 201 301 401 501; 102 202 302 402 502; 103 203 303 403 503; 104 204 304 404 504];
I was looking for an output as follows:
Final_data = [100 100 100
101 101 101
102 102 102
.. .. ..
.. .. ..
300 300 300
301 301 301
302 302 302
.. .. ..
.. .. ..
500 500 500
501 501 501
502 502 502
503 503 503
504 504 504 ]
@Mathan Like this?
a = 1:1:3;
c = [100 200 300 400 500; 101 201 301 401 501; 102 202 302 402 502; 103 203 303 403 503; 104 204 304 404 504];
final_data = repmat(c(:),1,numel(a));
disp(final_data)
100 100 100 101 101 101 102 102 102 103 103 103 104 104 104 200 200 200 201 201 201 202 202 202 203 203 203 204 204 204 300 300 300 301 301 301 302 302 302 303 303 303 304 304 304 400 400 400 401 401 401 402 402 402 403 403 403 404 404 404 500 500 500 501 501 501 502 502 502 503 503 503 504 504 504
Hi,
Well actually no - sorry if my second example was confusing. I have modified the original question now with a more suitable example which shows that the c matrix changes its value during the loop run and is a random matrix, so infact I really cannot use the repmat(). What I was looking for was a way to append the values from c matrix to final_data as rows without overriding the values.

Sign in to comment.

 Accepted Answer

Maybe something like this?
a = 2:1:5;
b = [1 1.2 2 2.3 3 3.3 4 5];
na = numel(a);
nb = numel(b);
nc = 3;
final_data = NaN(nb*nc,na);
for ii=1:na
c = rand(nc,nb) % showing c on command-line for reference
for jj=1:nb
if (a(ii) > b(jj))
final_data((jj-1)*nc+(1:nc),ii) = c(:,jj);
end
end
end
c = 3×8
0.7756 0.0228 0.8194 0.5967 0.9381 0.2879 0.0092 0.5636 0.6957 0.2733 0.8883 0.8130 0.1560 0.2556 0.6728 0.6247 0.6122 0.4209 0.3791 0.1853 0.5323 0.5289 0.8371 0.9574
c = 3×8
0.3283 0.4400 0.7155 0.8591 0.2053 0.6418 0.8155 0.1471 0.0725 0.2098 0.3402 0.0290 0.3501 0.8085 0.3803 0.9335 0.2055 0.6950 0.4742 0.4666 0.6381 0.4812 0.3803 0.6402
c = 3×8
0.0360 0.5393 0.2289 0.1805 0.0598 0.8810 0.8914 0.7146 0.7247 0.0987 0.1058 0.2307 0.6649 0.0273 0.8709 0.4841 0.1058 0.6794 0.2373 0.1903 0.3412 0.3625 0.4358 0.4167
c = 3×8
0.2973 0.0796 0.7687 0.6992 0.1326 0.6452 0.0892 0.1665 0.4848 0.9148 0.6581 0.6309 0.0303 0.6834 0.3425 0.4914 0.7989 0.3075 0.1044 0.1785 0.6989 0.8545 0.1679 0.0347
disp(final_data)
0.7756 0.3283 0.0360 0.2973 0.6957 0.0725 0.7247 0.4848 0.6122 0.2055 0.1058 0.7989 0.0228 0.4400 0.5393 0.0796 0.2733 0.2098 0.0987 0.9148 0.4209 0.6950 0.6794 0.3075 NaN 0.7155 0.2289 0.7687 NaN 0.3402 0.1058 0.6581 NaN 0.4742 0.2373 0.1044 NaN 0.8591 0.1805 0.6992 NaN 0.0290 0.2307 0.6309 NaN 0.4666 0.1903 0.1785 NaN NaN 0.0598 0.1326 NaN NaN 0.6649 0.0303 NaN NaN 0.3412 0.6989 NaN NaN 0.8810 0.6452 NaN NaN 0.0273 0.6834 NaN NaN 0.3625 0.8545 NaN NaN NaN 0.0892 NaN NaN NaN 0.3425 NaN NaN NaN 0.1679 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

5 Comments

But the non-NaN values in a column might be discontiguous if a or b is not monotonic:
a = 2:1:5;
% b = [1 1.2 2 2.3 3 3.3 4 5];
b = [1 1.2 4 5 3 3.3 2 2.3];
na = numel(a);
nb = numel(b);
nc = 3;
final_data = NaN(nb*nc,na);
for ii=1:na
c = rand(nc,nb) % showing c on command-line for reference
for jj=1:nb
if (a(ii) > b(jj))
final_data((jj-1)*nc+(1:nc),ii) = c(:,jj);
end
end
end
c = 3×8
0.1083 0.2044 0.3773 0.6838 0.2006 0.4139 0.3873 0.1343 0.6272 0.4463 0.5049 0.1458 0.0585 0.8073 0.8747 0.1989 0.6327 0.2310 0.2846 0.7523 0.9923 0.8388 0.5630 0.1548
c = 3×8
0.7268 0.5949 0.4476 0.5224 0.5686 0.7906 0.6267 0.0658 0.8050 0.6922 0.6834 0.3074 0.3411 0.7303 0.9056 0.8885 0.5745 0.1992 0.9910 0.5114 0.7064 0.4061 0.3204 0.6935
c = 3×8
0.8228 0.8223 0.7012 0.3833 0.8168 0.3479 0.2314 0.5737 0.1215 0.1456 0.9210 0.2621 0.1326 0.6175 0.6664 0.4939 0.5140 0.5830 0.2404 0.9799 0.5821 0.3683 0.2856 0.4276
c = 3×8
0.8164 0.1743 0.8823 0.3160 0.9020 0.0274 0.0497 0.9581 0.1325 0.6394 0.3810 0.0396 0.7746 0.1877 0.7071 0.1677 0.6500 0.9896 0.8840 0.5821 0.9976 0.8418 0.4942 0.0974
disp(final_data)
0.1083 0.7268 0.8228 0.8164 0.6272 0.8050 0.1215 0.1325 0.6327 0.5745 0.5140 0.6500 0.2044 0.5949 0.8223 0.1743 0.4463 0.6922 0.1456 0.6394 0.2310 0.1992 0.5830 0.9896 NaN NaN NaN 0.8823 NaN NaN NaN 0.3810 NaN NaN NaN 0.8840 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.8168 0.9020 NaN NaN 0.1326 0.7746 NaN NaN 0.5821 0.9976 NaN NaN 0.3479 0.0274 NaN NaN 0.6175 0.1877 NaN NaN 0.3683 0.8418 NaN 0.6267 0.2314 0.0497 NaN 0.9056 0.6664 0.7071 NaN 0.3204 0.2856 0.4942 NaN 0.0658 0.5737 0.9581 NaN 0.8885 0.4939 0.1677 NaN 0.6935 0.4276 0.0974
And I guess you'd want those non-NaNs at the bottom of columns 2-4 to be moved up?
oh yes this was something which I was looking for! Thank you - well I would be fine with intermediate nan values in columns 2-4 since that would serve my purpose.
Thanks again
But it would be great if I could remove the leading NaNs and shift everything upwards - like for example if the 2nd column had begun with 3 NaNs and then followed by 0.3283, 0.0725, etc then is it possible to remove those leading NaNs so that my second column now begins with 0.3283, 0.0725, etc?
You can avoid having any NaNs above any non-NaN c values by keeping track of how many rows have been written to in each column:
a = 2:1:5;
% b = [1 1.2 2 2.3 3 3.3 4 5];
b = [1 1.2 4 5 3 3.3 2 2.3];
na = numel(a);
nb = numel(b);
nc = 3;
final_data = NaN(nb*nc,na);
last_row = zeros(1,na);
for ii=1:na
c = rand(nc,nb) % showing c on command-line for reference
for jj=1:nb
if (a(ii) > b(jj))
final_data(last_row(ii)+(1:nc),ii) = c(:,jj);
last_row(ii) = last_row(ii)+nc;
end
end
end
c = 3×8
0.8679 0.1344 0.7409 0.8991 0.6924 0.2131 0.9353 0.1963 0.1268 0.5915 0.1404 0.2434 0.2801 0.9089 0.8597 0.3815 0.4814 0.8942 0.4151 0.6657 0.6755 0.9031 0.7325 0.5550
c = 3×8
0.0562 0.0343 0.9638 0.9161 0.0169 0.6211 0.6252 0.8444 0.2833 0.2906 0.9503 0.5871 0.1685 0.9125 0.8346 0.3167 0.5431 0.7185 0.8937 0.3508 0.7467 0.8985 0.6085 0.8455
c = 3×8
0.2012 0.3699 0.6199 0.4750 0.9173 0.8522 0.9058 0.6655 0.4513 0.2709 0.4633 0.5649 0.7279 0.1773 0.4038 0.2587 0.3472 0.4188 0.9591 0.1697 0.4908 0.2343 0.5552 0.5674
c = 3×8
0.4964 0.4386 0.9688 0.5840 0.5509 0.1135 0.8336 0.3020 0.8426 0.6906 0.7833 0.6806 0.3329 0.6814 0.2129 0.4306 0.3220 0.0782 0.9554 0.0162 0.3916 0.7909 0.6537 0.9954
disp(final_data);
0.8679 0.0562 0.2012 0.4964 0.1268 0.2833 0.4513 0.8426 0.4814 0.5431 0.3472 0.3220 0.1344 0.0343 0.3699 0.4386 0.5915 0.2906 0.2709 0.6906 0.8942 0.7185 0.4188 0.0782 NaN 0.6252 0.9173 0.9688 NaN 0.8346 0.7279 0.7833 NaN 0.6085 0.4908 0.9554 NaN 0.8444 0.8522 0.5509 NaN 0.3167 0.1773 0.3329 NaN 0.8455 0.2343 0.3916 NaN NaN 0.9058 0.1135 NaN NaN 0.4038 0.6814 NaN NaN 0.5552 0.7909 NaN NaN 0.6655 0.8336 NaN NaN 0.2587 0.2129 NaN NaN 0.5674 0.6537 NaN NaN NaN 0.3020 NaN NaN NaN 0.4306 NaN NaN NaN 0.9954 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 May 2022

Commented:

on 15 May 2022

Community Treasure Hunt

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

Start Hunting!