Using for-loop to extract data from every column in a matrix

37 ビュー (過去 30 日間)
Edward Keavney
Edward Keavney 2022 年 5 月 30 日
回答済み: Image Analyst 2022 年 5 月 30 日
Hi,
I'm new to using for-loops - can anyone help with my problem.
I have a matrix, 18000X31. I want to be able to write a for loop that for each column, a new variable is created that includes the data from the rows 3000-13600. Any help would be gratefully appreciated.
Thanks in advance!
  5 件のコメント
Edward Keavney
Edward Keavney 2022 年 5 月 30 日
@Walter Roberson Fantastic! Thanks for your help, this works perfectly!
Stephen23
Stephen23 2022 年 5 月 30 日
"I thought using a for-loop would help to allow me to step-by-step work through my problem."
There is no problem with using a FOR loop. Or reshaping, as Walter Roberson showed you.
But you should avoid creating "new variables" as you described in your question.

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

回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 5 月 30 日
M = randi([0 9], 180, 31); %example
letters = ['A':'Z' 'a' : 'z'];
numletters = length(letters);
for col = 1 : size(M, 2)
randname = letters( randi(numletters, 1, 32) );
tosave.(randname) = M(:,col);
end
tname = tempname() + ".mat";
save(tname, '-struct', 'tosave');
load(tname)
whos
Name Size Bytes Class Attributes BBvQHvyAMsruyGmPBCAtGFwBICGcDREY 180x1 1440 double FBWVzSBqpSgEroLkseFBwLDtWWXsxlxD 180x1 1440 double HkpsAkMtzUJwGFquzeVpqhJCkyMnwQIJ 180x1 1440 double HwsuyfFDnyaOprLHwveGAvIAPaahStOH 180x1 1440 double JpXqzfTZKfscSQxjQpzcyAXkGFrdNDaY 180x1 1440 double KwPHJsOOHqmeABtRqJNRPuIoWXKmptiq 180x1 1440 double KyHyAlLpoEqjClqVDHQSfSSChHZRxIQz 180x1 1440 double LuwGMmEbgbVnTxKWysgVqWQUBSAUOqNU 180x1 1440 double M 180x31 44640 double PSlMFIADpFbonEerutSludFKtrdVvpkd 180x1 1440 double RwdXMqsNjgGaWvbTWCkAXgcSUxHaxqiz 180x1 1440 double TcYHMTGMNhqxILMMhOcCxpJLfgcQbxJn 180x1 1440 double WYetiFJgfEdvjNNcCWMMjtSyxWXsBnCn 180x1 1440 double WcIWJBEpPcFTAtrQDARnBpINiiOzsLOO 180x1 1440 double YeOazlBpsMqHbPwnFUZpDFJEWqyGpuJi 180x1 1440 double YgdZRTlEDdUWbOfquxxuYplYuXjddeKX 180x1 1440 double ZmrbXkYCgqFjfNDsbNviFeyUPjKTgapb 180x1 1440 double aWsXNSxgZZheMVhkGZzcmtvqTJXBlaUv 180x1 1440 double abPmUvvAaIQHsRIYxmNhdeNWRjWimFLe 180x1 1440 double aiZCTrGsddtyrlpMiLJCMHVPxDQLyfex 180x1 1440 double asBxWZIEHuIPHhTSTepsOeRqpIjoOdcI 180x1 1440 double cmdout 1x33 66 char col 1x1 8 double jPXOPvgNgBpTvPeIdtZdzMzpYTDARNkb 180x1 1440 double kHVdaqCsycDjQeAhzsrxzpUwbyFWWsnc 180x1 1440 double lZsnQggmqCwhgprroPTIEXNYZDsljuKo 180x1 1440 double letters 1x52 104 char lxCLtgkKcHWuxyvObWQOvoesroyYGegT 180x1 1440 double numletters 1x1 8 double ooOGyGzeKmhUMXXGYDjehlkqaWsIaIzc 180x1 1440 double qujcfWwyEHTAhwEspdcUqxPxVjiFBYhb 180x1 1440 double randname 1x32 64 char sReqMWIVCMwWweFeFOJCcijOdpnHeNHi 180x1 1440 double tname 1x1 308 string tosave 1x1 49848 struct vIljIfxErWOCoshTfilYvCWfCDtGestN 180x1 1440 double vpvYyvIjGYuBFtMTrAiywIlmjtUMQrNq 180x1 1440 double wFiDXZhNtaSNBGFScNPHNCbgKrZkmrTf 180x1 1440 double xmPutkbkEHAkWqvJqyZljSFohnXBrQyn 180x1 1440 double
Now what? What are you going to do with sReqMWIVCMwWweFeFOJCcijOdpnHeNHi now that you have it?

Image Analyst
Image Analyst 2022 年 5 月 30 日
@Edward Keavney, do this for your matrix m:
[rows, columns] = size(m)
for col = 1 : columns
fprintf('Processing column %d of %d.\n', col, columns);
% Create a new variable with only this particular column,
% but only with rows 3000 to 13600:
thisColumn = m(3000 : 13600, col);
% Now process this column somehow,
% like getting its mean or whatever you want to do.
end
fprintf('All done!\n');

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by