resize matrix based on indices

2 次查看(过去 30 天)
Hi,
I am working with matrices as in the file attached below. The first 4 columns log for stimuli representation in time. The rows are time and the columns are events. The first column logs stimuli id in time. (No stimulus =0, stimulus : 1 2 3 4 5 or 6) The second column logs the stages (1= pre stimulus, 2=Stimulus, 3= post stimulus ); Column 3 logs duration and ID of each trial. Trial 1 ==1; Trial trial 2 ==2; etc I need to select given trials ids. For example, take trials [2 5 7 10] concatenate them back in to matrix and save as another file. Can anyone help with it?

采纳的回答

Voss
Voss 2023-11-3
input_file = 'logfile_example-30-09-23 .xlsx';
output_file = 'trials_2_5_7_10_only.xlsx';
ids = [2 5 7 10];
id_column = 3;
T = readtable(input_file);
T_new = T(ismember(T.(id_column),ids),:);
writetable(T_new,output_file)
  2 个评论
EK
EK 2023-11-3
Thank you so much! I have a question to your code. Is there way to save excel file without headers (var 1, var 2, var 3, ...)?
Stephen23
Stephen23 2023-11-3
"Is there way to save excel file without headers (var 1, var 2, var 3, ...)?"
Read about WriteVariableNames in the WRITETABLE documentation

请先登录,再进行评论。

更多回答(1 个)

Constantino Carlos Reyes-Aldasoro
This is an example of what is called "addressing a matrix", i.e., you need to use the address of certain locations of a matrix in matlab. I will copy part of your data and adapt to illustrate
data = [ 0 1 1 1 1.991688728 1.103503108 2.441900969 0.566304922 1.489537835 1.773482203 1.95588553 ;...
0 1 1 2 1.783852696 1.137677789 2.268280506 0.54307586 1.646252394 1.575202107 1.591086984 ;...
0 1 1 3 1.958541274 1.029714346 2.508745909 0.563571632 1.846422434 1.870307446 1.568911672 ;...
0 1 1 4 1.994016409 1.224192739 2.379162073 0.378690213 1.639967084 1.7403965 2.070030212 ;...
8 1 1 5 1.925235748 1.208242774 2.574904919 0.450008661 1.614949703 2.012422562 1.788136601 ;...
8 1 1 6 1.940020323 1.030568957 2.674375296 0.292536885 1.561530113 1.818652987 1.704922438 ;...
0 1 1 7 1.63369894 1.03463912 2.50246048 0.289227307 1.552333713 1.539394379 1.914013505 ;...
8 1 1 8 1.975015521 1.269600391 2.304613352 0.469067484 1.385410786 1.596593976 1.827103496 ;...
9 1 1 9 1.736701131 0.980502307 1.776961684 0.474588722 1.531394362 1.590884686 1.521726131 ;...
9 1 1 10 1.74925375 0.972486496 2.514164209 0.501453996 1.752897501 1.724050045 1.440591812 ;...
0 1 1 11 1.835679412 1.267914653 2.304520369 0.302720666 1.587394953 1.592901349 1.91214478 ];
If you know the address (rows and columns) that you need, then it is very easy to retrieve those values, for instance rows 1 and 2, and columns 2 to 4
data(1:2, 2:4)
ans = 2×3
1 1 1 1 1 2
Or all values of the third row:
data(3,:)
ans = 1×11
0 1.0000 1.0000 3.0000 1.9585 1.0297 2.5087 0.5636 1.8464 1.8703 1.5689
Notice how the addresses are passed as row, column.
When you do not know the exact address, but you know the values that define those addresses, like the first column, you can search for the values and use those as indices, e.g. take first five columns of all rows that have an 8 in the first column:
data( data(:,1)==8, 1:5)
ans = 3×5
8.0000 1.0000 1.0000 5.0000 1.9252 8.0000 1.0000 1.0000 6.0000 1.9400 8.0000 1.0000 1.0000 8.0000 1.9750
Or take the first 8 columns of those rows that have a value less than 0.3 in the column 8. Now, also save into a new variable:
data2 = data( data(:,8) < 0.3, 1:8)
data2 = 2×8
8.0000 1.0000 1.0000 6.0000 1.9400 1.0306 2.6744 0.2925 0 1.0000 1.0000 7.0000 1.6337 1.0346 2.5025 0.2892
In that way, you can select any regions of your data and then save them as the new variable.
Hope this helps you solve your problem. If you need a longer description, the first chapter of my book has more details:
https://www.wiley.com/en-us/Biomedical+Image+Analysis+Recipes+in+MATLAB:+For+Life+Scientists+and+Engineers-p-9781118657447

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by