Asked by John
on 24 Jan 2012

Hi there,

I'm using the code below to loop through files in a folder and classifying data with a neural network.

files = dir(fullfile(pwd,'*.txt')); for k = 1 : numel(files) %%%% reading the txt file fid01 = fopen(fullfile(pwd,files(k).name)); idx = 0; tmparray = []; tline = fgetl(fid01); while ischar(tline) idx = idx + 1; tmparray(idx,:) = str2num(tline); tline = fgetl(fid01); end fclose(fid01); %%%% simulate net output = sim(net, tmparray); %%%% save the output filename = strcat(regexprep(files(k).name,'.txt',''),'-output.txt'); fid02 = fopen(filename,'w'); fprintf(fid02,'%.2f\n',output'); fclose(fid02); end

However the output is in the form of a 1x1 array, for example 1x1 array. For example

output = [ 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]

Would it be possible to convert it into a sparse matrix like this

output = full(output)

0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

and then convert it to vectors , for example

Output = vec2ind(output)

2 2 2 3

I'm trying to get the output in integer format.

I would really appreciate any help

Thank you

John

*No products are associated with this question.*

Answer by Greg Heath
on 25 Jan 2012

For a net with I-H-O node topology, an input matrix of N I-dimensional input column vectors has size(input) = [ I N ] . The command output = sim(net,input) will yield an output matrix of N O-dimensional output column vectors with size(output) = [ O N ]. If output is binary, with at least one "1" in each column, vec2ind(output) will yield a row vector containing the row numbers of each unit element. and full(ind2vec(result)) will recover the original matrix.

rand('state',4151941)

output = round(rand(3,4))

result = vec2ind(output)

output = [ 1 0 0 0; 1 1 1 0; 0 0 1 1]

result = [ 1 2 2 2 3 3 ]

full(ind2vec(result)) = [ 1 0 0 0; 1 1 1 0; 0 0 1 1]

Hope this helps.

Greg

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments