How to add rows to the matrix

7 ビュー (過去 30 日間)
EK
EK 2022 年 12 月 17 日
コメント済み: EK 2022 年 12 月 18 日
Hallo,
I have a matrix of 6 columns (an example attached here) that are : trials, stimulus IDs, Hit, Miss, CR, FA.
Hit, Miss, CR, FA colomns are coded as a 0 and 1 but the FA column has sometimes numbers larger than 1 that indicates number of repeated trials that are not included in to the matrix. I need correct this and everywhere when FA colomn has number > 1 replace it with repeated rows in a number of repeats. For example, in given example in the trial 6 FA has number of repeats 5. I need to replace it with 5 consequentive rows after 6th trial (trials 7-11) in which stimulus is the same, Hit, Miss, CR, would have 0 and FA column 1. Same is for trial 20 (add 3 repeated trials) and 29 (add 2 repeated trials) etc
Can anyone help with this?
  1 件のコメント
Jan
Jan 2022 年 12 月 17 日
You have attached an Excel file. Is importing this file a part of the problem? What is the wanted output? A Matlab matrix or another Excel file?
Does "raws" mean "rows"?

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

採用された回答

Torsten
Torsten 2022 年 12 月 17 日
編集済み: Torsten 2022 年 12 月 18 日
Like this ?
A = [1 4 0 0 1 0
2 3 0 1 0 0
3 3 0 1 0 0
4 4 0 0 1 1
5 3 0 1 0 0
6 4 0 0 1 5
7 3 0 1 0 0
8 3 1 0 0 0
9 4 0 0 1 0
10 4 0 0 0 1];
B = [];
irow = 0;
for i = 1:size(A,1)
B(irow+1,:) = [irow+1,A(i,2:6)];
irow = irow + 1;
trials = A(i,6);
if trials ~= 0 && trials ~= 1
for j = 1:trials
B(irow+j,:) = [irow+j 0 0 0 0 1];
end
irow = irow + trials;
end
end
B(1:10,:)
ans = 10×6
1 4 0 0 1 0 2 3 0 1 0 0 3 3 0 1 0 0 4 4 0 0 1 1 5 3 0 1 0 0 6 4 0 0 1 5 7 0 0 0 0 1 8 0 0 0 0 1 9 0 0 0 0 1 10 0 0 0 0 1
B(11:15,:)
ans = 5×6
11 0 0 0 0 1 12 3 0 1 0 0 13 3 1 0 0 0 14 4 0 0 1 0 15 4 0 0 0 1
  6 件のコメント
Torsten
Torsten 2022 年 12 月 18 日
A = [1 4 0 0 1 0
2 3 0 1 0 0
3 3 0 1 0 0
4 4 0 0 0 1
5 3 0 1 0 0
6 4 0 0 0 5
7 3 0 1 0 0
8 3 1 0 0 0
9 4 0 0 1 0
10 4 0 0 0 2
11 4 0 0 1 0
12 3 0 1 0 0
13 3 0 1 0 0
14 4 0 0 0 1];
B = [];
irow = 0;
for i = 1:size(A,1)
trials = A(i,6);
if trials ~= 0 && trials ~= 1
for j = 1:trials
B(irow+j,:) = [irow+j,A(i,2:5),1];
end
irow = irow + trials;
else
B(irow+1,:) = [irow+1,A(i,2:6)];
irow = irow + 1;
end
end
B(1:10,:)
ans = 10×6
1 4 0 0 1 0 2 3 0 1 0 0 3 3 0 1 0 0 4 4 0 0 0 1 5 3 0 1 0 0 6 4 0 0 0 1 7 4 0 0 0 1 8 4 0 0 0 1 9 4 0 0 0 1 10 4 0 0 0 1
B(11:end,:)
ans = 9×6
11 3 0 1 0 0 12 3 1 0 0 0 13 4 0 0 1 0 14 4 0 0 0 1 15 4 0 0 0 1 16 4 0 0 1 0 17 3 0 1 0 0 18 3 0 1 0 0 19 4 0 0 0 1
EK
EK 2022 年 12 月 18 日
Thank you so much! You are amazing!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by