Create matrix using hashtable MATLAB - matlab

I am using hashtable (containers.map ) in MATLAB, and now I want to create a matrix with that information. So when I run my hashtable and then insert my text files for each type I get on my command window something like that:
edit
F = listdlg('PromptString','Different types', 'SelectionMode',...
'single', 'ListString',E, 'Name','Select a type','ListSize',[230 130]);
[files,path] = uigetfile ('*.txt','Select your text files',...
'MultiSelect','on');
where E is just the user's input which in this case is pink, purple and yellow.
%save for each type the user enters the corresponding text files he
%wants to train
%A Map object is a data structure that allows you to retrieve values
%using a corresponding key. Keys can be real numbers or text strings
%and provide more flexibility for data access than array indices,
%which must be positive integers. Values can be scalar or nonscalar arrays.
handles.map (E(F,:)) = files;
handles.map
a = handles.map.values
b = handles.map.keys
handles.map.size
b =
{1x3 cell} {1x6 cell} {1x4 cell}
a =
'pink ' 'purple' 'yellow'
So I want now to the the total number of b to be the number of rows on my matrix m ; so a total number of rows 14 and each bit from a to be a column; so a total of 3 columns. But I want to create a binary matrix where each column will identify different type. Finally I will have created a matrix like that:
m =[1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 0 1
0 0 1];
Where the first 3 rows of the matrix says that there are 3text files of type pink, the next 6 rows : 6 text files of type purple and the last 4: 4 text files of type yellow.
I hope that now is more clear. :)
Any help would be appreciated! :) x

So something like this?
val = cellfun(#length, b)';
m = 0;
for v = 1:size(val)
m(end:end+val(v)-1,v) = 1;
end

Related

Combinations of zeros and ones of vector size n [duplicate]

I have an algorith that the number of possibles combinations of 0 and 1, can reach the number 2^39. Let's say i have n=2 situations, or n1=2^2=4 combinations of 0 and 1: 00,01,10,11.From that i can create an array a=zeros(n,n1) and fill the columns with the possible combinations? That means first column has 00,second 01,third 10,last 11.I want this to be dynamic that means that n can be 1,2,3...,39, show the array will be a=zeros(n,2^n).Thanks for any response!
Just for general understanding: why do you think you need an array of all combinations of all integers from 0 to 2³⁹? That array would consume 39×2³⁹/1000⁴ ≈ 21TB of RAM...last time I checked, only the world's most advanced supercomputers have such resources, and most people working with those machines consider generating arrays like this quite wasteful...
Anyway, for completeness, for any N, this is the simplest solution:
P = dec2bin(0:(2^N)-1)-'0'
But, a little piece of advice: dec2bin outputs character arrays. If you want numerical arrays, you can subtract the character '0', however, that gives you an array of doubles according to the rules of MATLAB:
>> P = dec2bin(0:(2^3)-1)-'0';
>> whos P
Name Size Bytes Class Attributes
P 8x3 192 double
If you want to minimize your memory consumption, generate a logical array instead:
>> P = dec2bin(0:(2^3)-1)=='1';
>> whos P
Name Size Bytes Class Attributes
P 8x3 24 logical
If you want to also speed up the execution, use the standard algorithm directly:
%// if you like cryptic one-liners
B1 = rem(floor((0:pow2(N)-1).' * pow2(1-N:0)), 2) == 1;
%// If you like readability
B = false(N,pow2(N));
V = 0:pow2(N)-1;
for ii = 1:N
B(ii,:) = rem(V,2)==1;
V = (V-B(ii,:))/2;
end
That last one (the loop) is fastest of all solutions for any N (at least on R2010b and R2013a), and it has the smallest peak memory (only 1/Nth of the cryptic one-liner).
So I'd go for that one :)
But, that's just me.
Using ndgrid with a comma-separated list as output (see also here):
[c{1:N}] = ndgrid(logical([0 1]));
c = cat(N+1,c{N:-1:1});
c = reshape(c,[],N);
Example: N=4 gives
c =
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1

Rearranging the non zero entries in a tensor into a matrix

I have a NxNx5 array T that I would like to convert into a Rx5 array TT such that the following condition is satisfied (where R is the number of non-zero entries of the array T(:,:,1)):
If T(i,j,1) == 0 then we ignore. If T(i,j,1) != 0 then I would like a row of TT whose entry is
[T(i,j,1) T(i,j,2) T(i,j,3) T(i,j,4) T(i,j,5)]
Note that T(i,j,k) (k = 2,3,4,5) could be zero. For example,
If
T(3,2,1) = 3
then I would like a row of TT to be
[3 0 2 1 5].
Some notes:
The entries of TT are all integers.
The entries accent in order column wise. i.e the first column of TT(:,:,1) maybe
[1 2 0 0 3 4 0 0 0 5 6]'
then the next column
[7 8 0 0 0 0 0 9 10 11 12]'
I think this does what you want:
ind = find(T(:,:,1));
ind = bsxfun(#plus, ind(:), (0:size(T,3)-1)*size(T,1)*size(T,2));
result = T(ind);
This will do it:
clear
rng(343)
N=7;
K=5;
T=randi([0,4],[N,N,K])
TT=reshape(T,[N*N,K])
TT(T(:,1)==0,:)=[] %delete rows with first col equal to 0

Matlab: enter same vector repeatedly to matrix using logical indexing

I would like to enter the same vector of numbers repeatedly to an existing matrix at specific (row) logical indices. This is like an extension of entering just a single number at all logical index positions (at least in my head).
I.e., it is possible to have
mat = zeros(5,3);
rowInd = logical([0 1 0 0 1]); %normally obtained from previous operation
mat(rowInd,1) = 15;
mat =
0 0 0
15 0 0
0 0 0
0 0 0
15 0 0
But I would like to do sth like this
mat(rowInd,:) = [15 6 3]; %rows 2 and 5 should be filled with these numbers
and get an assignment mismatch error.
I want to avoid for loops for the rows or assigning vector elements single file. I have the strong feeling there is an elementary matlab operation that should be able to do this? Thanks!
The problem is that your indexing picks two rows from the matrix and tries to assign a single row to them. You have to replicate the targeted row to fit your indexing:
mat = zeros(5,3);
rowInd = logical([0 1 0 0 1]);
mat(rowInd,:) = repmat([15 6 3],sum(rowInd),1)
This returns:
mat =
0 0 0
15 6 3
0 0 0
0 0 0
15 6 3

convert string to binary matrix with 1 bit per column

How do I convert characters from a file, e.g. 'hello' to a binary matrix like this:
[1 1 0 1 1 0 0 0 1 0 .......]
where each column of the matrix has only a 1 bit value that is 0 or 1.
All I have done so far is converted the string into the matrix of binary where each column has 7 bits of binary.
Example: 'hello'
1 1 0 1 1 0 0 0 1 0
You need a combination of dec2bin and str2num:
First, convert your input into a binary representation:
WORD = 'hello';
WORD_BINARY = dec2bin(WORD,7) % The 7 gives the number of bits
This results in:
WORD_BINARY =
1101000
1100101
1101100
1101100
1101111
This is a string, which now has to be turned into a vector:
for i=1:size(WORD_BINARY,1)
for j=1:size(WORD_BINARY,2)
WORD_OUTPUT(1,(i-1)*size(WORD_BINARY,2)+j) = str2num(WORD_BINARY(i,j))
end
end
WORD_OUTPUT in this case is a <1x40> vector, starting with:
WORD_OUTPUT =
[ 1 1 0 1 0 ...
Edit
If you do not want two for loops, you can use reshape first (but be aware, that reshape orders by column, not row):
WORD = 'hello';
WORD_BINARY = reshape(dec2bin(WORD,7)',1,[]);
% note the <'> after the dec2bin, to transpose the matrix
for j=1:size(WORD_BINARY,2)
WORD_OUTPUT(1,j) = str2num(WORD_BINARY(1,j));
end

Grouping strings by matching substring

After running a SPICE simulation I get an .a2d file which I am parsing with Matlab. After parsing the file I get a cell array with the names of the variables
Ex: vars = {'s0';'s1';'s2';'a0';'a1'}
and a data matrix with the signals transitions, where each row represents the data of each signal and columns represent time. All the data is binary, i.e., it's only 0 or 1.
What I want to do is to create an algorithm that detects "words" based on the names stored in vars. For example, s0, s1, and s2 form a 3-bit word 's'; a0 and a1 a 2-bit word 'a'.
Finally, what I need is to break the data matrix into one array for each word (converting from binary to decimal).
I am doing that by hand, but I want to know if there is any way to do it by a script.
Here's a possible solution, putting the final values into the fields of a struct. I've used a regexp to extract the variable names and shifts as you didn't say how long they might be.
% fake data
vars = {'sums4', 'sums2', 'sums1', 'a0', 'a5'};
data(1,:) = logical([0 0 1 0 0 0 1]);
data(2,:) = logical([0 1 1 1 1 0 0]);
data(3,:) = logical([1 0 1 1 0 0 1]);
data(4,:) = logical([0 0 1 0 1 1 1]);
data(5,:) = logical([0 1 1 1 0 0 0]);
output = struct();
for i = 1:length(vars)
matches = regexp(vars{i}, '(\D+)(\d+)', 'tokens');
var = matches{1}{1};
shiftsize = str2num(matches{1}{2});
% initialise if not already present
if (~isfield(output, var))
output.(var) = zeros(1, size(data, 2));
end
% add apropriate shifted value to current values
output.(var) = output.(var) + (bitshift(1, shiftsize) * data(i, :));
end
output ends up with fields named after the string parts of vars
output =
sums: [2 4 22 6 4 0 18]
a: [0 32 33 32 1 1 1]