How can I convert indices into a binary matrix?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MByk
el 18 de Feb. de 2024
Comentada: Alexander
el 18 de Feb. de 2024
Hello all, I have a txt file that includes list of selected items (there are 10 items in total). I am reading the file using "readmatrix". I want to create a binary (logical) matrix as in the example below but I am getting "The subscript vectors must all be of the same size." message. How can I fix it? Thanks for the help.
1,3,5 -> 1,0,1,0,1,0,0,0,0,0
1,2,3,7 -> 1,1,1,0,0,0,1,0,0,0
1,2,6,7,8 -> 1,1,0,0,0,1,1,1,0,0
1,2,3 -> 1,1,1,0,0,0,0,0,0,0
...
Respuesta aceptada
Dyuman Joshi
el 18 de Feb. de 2024
Editada: Dyuman Joshi
el 18 de Feb. de 2024
ItemSet = readmatrix('Test.txt','NumHeaderLines',0)
tic
s = size(ItemSet);
%corresponding row values
row = repelem((1:s(1)).', 1, s(2))
%maximum value
m = max(ItemSet, [], 'all');
%subscript values
idx = [row(:) ItemSet(:)];
%check for NaN values
k = isnan(idx(:,2));
out = accumarray(idx(~k,:), 1, [s(1) m])
toc
Más respuestas (1)
Alexander
el 18 de Feb. de 2024
Maybe something like the following?
fid = fopen('Item.txt');
A = [];
while(~feof(fid))
syLine = fgetl(fid);
dyA = zeros(1,10);
syLine = ['[' syLine ']'];
dyLine = eval(syLine);
dyA(dyLine) = 1;
A = [A; dyA];
end
A
fclose(fid)
3 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!