storing for loop answer into matrix

조회 수: 1 (최근 30일)
Cside
Cside 2019년 10월 28일
댓글: Bhaskar R 2019년 10월 28일
Hi, i have this code and it gives me 8 answers of matrix "locations". Is there a way I can write another line of code to store the loop answers into a matrix? The end result should be a 1 x 8 cell array.
Thanks!
for i = 1:8
fefinsessionn = find(sessions_fef==i); %%extract the fef neurons that appear in the selected trials
trialsize = size(trials(i).val); %%number of trials in the selected session (i only want columns?)
A = dataset_fef_sac_all(fefinsessionn,:); %%selected rows/neurons in mean fef dataset
A(:,(trialsize(:,2)+1):621) = [];
B = ix_all(i); %%based off i, to select the respective ix, to use {ix .... ix_s8}, create ixall matrix
B2 = cell2mat(B);
[locations,~] = find(B2);%%location of target in respective ix
end

채택된 답변

Bhaskar R
Bhaskar R 2019년 10월 28일
Yes you can write by adding the loop variavle value to index of the locations.
If the result of the find(B2) is same length for each iteration then you can assign directly loop variable index to the locations(ii) by initializing empty array in the begining of the loop. If the result length is different each time you can use locations as cell array as shown.
locations = cell(1, 8)
for i = 1:8
fefinsessionn = find(sessions_fef==i); %%extract the fef neurons that appear in the selected trials
trialsize = size(trials(i).val); %%number of trials in the selected session (i only want columns?)
A = dataset_fef_sac_all(fefinsessionn,:); %%selected rows/neurons in mean fef dataset
A(:,(trialsize(:,2)+1):621) = [];
B = ix_all(i); %%based off i, to select the respective ix, to use {ix .... ix_s8}, create ixall matrix
B2 = cell2mat(B);
[locations{i},~] = find(B2);%%location of target in respective ix
end
To access data from locations use notation as locations{1}, locations{2}...locations{8} for each loop data
  댓글 수: 1
Bhaskar R
Bhaskar R 2019년 10월 28일
Answer of the Daniel M is recommended for thorough understanding of the indexing in the loop iteration

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Daniel M
Daniel M 2019년 10월 28일
It would often be much faster for you to just look up the documentation than to post a question here and wait for an answer. I suggest learning to use the documentation as your first course of action when you don't know something.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by