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

2 ビュー (過去 30 日間)
Mathan
Mathan 2022 年 5 月 14 日
コメント済み: Mathan 2022 年 5 月 15 日
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 件のコメント
Voss
Voss 2022 年 5 月 14 日
@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
Mathan
Mathan 2022 年 5 月 14 日
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.

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 5 月 14 日
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 件のコメント
Voss
Voss 2022 年 5 月 15 日
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
Mathan
Mathan 2022 年 5 月 15 日
Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by