How could I have each result as a vector? - matlab

I think that I have always the same problem, vector, array and cell array, Thanks to this discussion, How can I split a Text in blocks of 16 bytes every one? I could resolve my first problem. However I still need that each result in data must be e vector in order to encrypt it.
fid = fopen('file.txt', 'r');
alldata = textscan(fid, '%s');
tmp = reshape(alldata{1}, 16, []).';
tmp = arrayfun(#(x)strjoin(tmp(x,:)), 1:size(tmp, 1), 'uniformoutput', false)
key=hex2dec(key_hex).'
data= (cat(1, tmp{:}))
for i= 1:rows(data)
Matrix(i, :)= hex_keys([data(i,1:15), data(i,16)])
chiffrement (Matrix(i,:), key,1)
endfor
endfunction
My error is: error: Plaintext has to be a vector (not a cell array) with 16 elements.
I would be very grateful if you could help me.
The file.txt contains for example:
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81 60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81 60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
In fact, it is a big programm and succesion of function, All what i need is how to convert each line in this data result to a vector.
data =
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81

I think you are over complicating the problem, my assumptions is you want to convert your data in file.txt into numbers (i.e. you used hex2dec), so let's do just that and leave Arrayfun out of the problem:
fid = fopen('file.txt', 'r');
alldata = textscan(fid, '%s');
tmp = reshape(alldata{1}, 16, []).'; % here we still parse 16 hex for every row using your function call
tmp = cellfun(#hex2dec,tmp,'un',0) % now we use cellfun to convert all your hex to numbers
Matrix = cell2mat(tmp)
Matrix =
96 61 235 16 21 202 113 190 43 115 174 240 133 125 119 129
96 61 235 16 21 202 113 190 43 115 174 240 133 125 119 129
96 61 235 16 21 202 113 190 43 115 174 240 133 125 119 129
whos Matrix
Name Size Bytes Class Attributes
Matrix 3x16 384 double
Now you can use your for loop to do whatever you want, it becomes regular indexing.

Related

Matlab: select submatrix from matrix by certain criteria

I have a matrix A
A=[f magic(10)]
A=
931142103 92 99 1 8 15 67 74 51 58 40
931142103 98 80 7 14 16 73 55 57 64 41
931142103 4 81 88 20 22 54 56 63 70 47
459200101 85 87 19 21 3 60 62 69 71 28
459200101 86 93 25 2 9 61 68 75 52 34
459200101 17 24 76 83 90 42 49 26 33 65
459200101 23 5 82 89 91 48 30 32 39 66
37833100 79 6 13 95 97 29 31 38 45 72
37833100 10 12 94 96 78 35 37 44 46 53
37833100 11 18 100 77 84 36 43 50 27 59
The first column are firm codes. The rest columns are firms' data, with each row referring to the firm in Column 1 in a given year. Notice that years may not be balance for every firms.
I would like to subtract sub-matrices according to the first column. For instance, for A(1:3,2:11) for 931142103:
A(1:3,2:11)
ans =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
Same as 459200101 (which would be A(4:7,2:11)) and A(8:10,2:11) for 37833100.
I get a sense that the code should like this:
indices=find(A(:,1));
obs=size(A(:,1));
for i=1:obs,
if i==indices(i ??)
A{i}=A(??,2:11);
end
end
I have difficulties in indexing these complicated codes: 459200101 and 37833100 in order to gather them together. And how can I write the rows of my submatrix A{i}?
Thanks so much!
One approach with arrayfun -
%// Get unique entries from first column of A and keep the order
%// with 'stable' option i.e. don't sort
unqA1 = unique(A(:,1),'stable')
%// Use arrayfun to select each such submatrix and store as a cell
%// in a cell array, which is the final output
outA = arrayfun(#(n) A(A(:,1)==unqA1(n),:),1:numel(unqA1),'Uni',0)
Or this -
[~,~,row_idx] = unique(A(:,1),'stable')
outA = arrayfun(#(n) A(row_idx==n,:),1:max(row_idx),'Uni',0)
Finally, you can verify results with a call to celldisp(outA)
If values in column 1 always appear grouped (as in your example), you can use mat2cell as follows:
result = mat2cell(A, diff([0; find(diff(A(:,1))); size(A,1)]));
If they don't, just sort the rows of A according to column 1 before applying the above:
A = sortrows(A,1);
result = mat2cell(A, diff([0; find(diff(A(:,1))); size(A,1)]));
If you don't mind the results internally not being ordered, you can use accumarray for this:
[~,~,I] = unique(A(:,1),'stable');
partitions = accumarray(I, 1:size(A,1), [], #(I){A(I,2:end)});

How to reshape a matrix horizontally using MATLAB

I have matrix A of the size(4,192). It consists of 12 matrices of the size(4,4) aligned horizontally. I want to get matrix B with the size(12,16). B must get as follows:
suppose
A=[y1,y2,y3,...,y12]
in which yn is a 4*4 matrix. Then,
B=[y1,y4,y7,y10;
y2,y5,y8,y11;
y3,y6,y9,y12]
Is there an efficient/quicker (using no loop) way to do this using MATLAB?
You can try the following code:
ys1 = 2; % size(1) from submatrix (for the following example, use ys1 = 4 for the actual problem)
ys2 = 2; % size(2) from submatrix (for the following example, use ys2 = 4 for the actual problem)
ns1 = 3; % size(1) of final matrix in terms of submatrix (3 rows)
ns2 = 4; % size(2) of final matrix in terms of submatrix (4 columns)
temp = reshape(A,ys1,ys2,ns1,ns2);
B = reshape(permute(temp,[1 3 2 4]),ys1*ns1,ys2*ns2);
Example:
A = [11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 91 92 101 102 111 112 121 122;
13 14 23 24 33 34 43 44 53 54 63 64 73 74 83 84 93 94 103 104 113 114 123 124];
B =
11 12 41 42 71 72 101 102
13 14 43 44 73 74 103 104
21 22 51 52 81 82 111 112
23 24 53 54 83 84 113 114
31 32 61 62 91 92 121 122
33 34 63 64 93 94 123 124

Load with octave created ASCII format .mat file in Matlab

As I am running out of licenses I am using both Matlab and Octave and as long as I keep things simple I haven't had any trouble.
I recently started using .mat files to decrease my amount of single files.
When I do everything in Matlab it works just fine but when I use 'save' in Octave it saves the file as ASCII and it looks somewhat like this at the beginning and has multiple matrixes in it and so multiple headers.
# Created by Octave 3.6.4, Mon Feb 24 21:34:39 2014 CET <***#****>
# name: C
# type: matrix
# rows: 10000
# columns: 10
79 79 79 79 79 79 79 79 79 79
74 115 87 55 101 46 83 92 113 61
69 142 128 48 160 45 87 113 114 71
84 107 145 62 245 78 69 88 149 78
120 73 148 32 299 114 57 79 137 76
This is fine but Matlab refuses to read the file. Neither with 'load' and '-ASCII' nor with importdata.
(Warning: File contains uninterpretable
data....)
Is there anything I can do? Octave loads the files just fine with 'load'.
Thanks!!
In Octave save as "-ascii" or as matlab binary format v4, v6, v7
octave:1> a = rand(3, 3)
a =
0.086093 0.541999 0.889222
0.029643 0.633532 0.762954
0.544787 0.150573 0.927285
octave:2> save ("-ascii", "yourfile.asc")
octave:3> save ("-v7", "yourfile.mat")
back in matlab do
>> b = load ('yourfile.asc')
b =
0.0861 0.5420 0.8892
0.0296 0.6335 0.7630
0.5448 0.1506 0.9273
or
>> load ('yourfile.mat')
>> a
a =
0.0861 0.5420 0.8892
0.0296 0.6335 0.7630
0.5448 0.1506 0.9273

Morphological dilation on Greyscale image using a neighborhood of size n x n in MATLAB

The Dilation process is performed by laying the structuring element B on the image A and sliding it across the image in a manner similar to convolution.
I understand the main concept behind the mathematical morphology dilation of the grayscale images, but i still have one question :
Can the values of the structure element be not chosen by the user ? In other words, can we perform a dilation process on the image by just choosing the size and shape of the structure element without specifying its elements?
For more precision, i will explain well my question by an example : Assume a greyscale image I of size 160 x 160 to be processed (dilated in this case) by a neighborhood of size 8 x 8. I didn't specify the elements of this neighborhood, so their elements are from the image itself. For example i wrote the matlab code below:
Max_image = max_filter(I, [0 0 7 7]);
Where the function max_filter is:
[n m] = size(I); % n=160 and m=160
B = I;
for i = 1:m-7,
B(:,i) = max(I(:,i:i+7),[],2);
end
for i=m-7+1:m
B(:,i) = max(I(:,i:min(end,i+7),[],2);
end
for i = 1:n-7,
I(i,:) = max(B(max(1,i):min(end,i+7),:),[],1);
end
for i = n-7+1:n,
I(i,:) = max(B(i:min(end,i+7),:),[],1);
end
Does that is still considered as a morphological dilation operation ? Recall that i used a structure element of size 8 x 8.
Your program is equivalent to a full image dilation with the structure element of ones(8) (btw, you didn't use the input argument [0 0 7 7], and you don't need that indeed):
I = [92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59];
Max_image = max_filter(I, [0 0 7 7]) will give you:
99 99 97 97 97 75 75 75 72 72
98 97 97 97 97 75 75 75 72 72
100 100 100 97 97 75 75 75 72 72
100 100 100 97 97 75 75 75 72 72
100 100 100 97 97 75 75 75 72 72
100 100 100 97 97 72 72 72 72 72
100 100 100 97 97 72 72 72 72 72
100 100 100 97 97 72 72 72 72 72
100 100 100 96 84 59 59 59 59 59
100 100 100 84 84 59 59 59 59 59
When you are using:
J1=imdilate(I,ones(8),'full');
J1(8:end,8:end)
It will give you exactly the same answer.
That's why I told you yesterday that it is often to use a binary image mask as the structure element. You don't need to choose the value inside the mask, but you need to choose the size (8*8) and shape. What is a shape? In a binary image, the elements that are filled with 1 determines the shape. Here in your code you selected the largest value within the 8*8 region, that is equivalent to the image dilation with a whole bright 8*8 square shaped mask.

Sliding window algorithm for activity recognition

I want to write a sliding window algorithm for use in activity recognition.
The training data is <1xN> so I'm thinking I just need to take (say window_size=3) the window_size of data and train that. I also later want to use this algorithm on a matrix
.
I'm new to matlab so i need any advice/directions on how to implement this correctly.
The short answer:
%# nx = length(x)
%# nwind = window_size
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;
idx will be a matrix of size nwind-by-K where K is the number of sliding windows (ie each column contains the indices of one sliding window).
Note that in the code above, if the last window's length is less than the desired one, it is dropped. Also the sliding windows are non-overlapping.
An example to illustrate:
%# lets create a sin signal
t = linspace(0,1,200);
x = sin(2*pi*5*t);
%# compute indices
nx = length(x);
nwind = 8;
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;
%'# loop over sliding windows
for k=1:size(idx,2)
slidingWindow = x( idx(:,k) );
%# do something with it ..
end
%# or more concisely as
slidingWindows = x(idx);
EDIT:
For overlapping windows, let:
noverlap = number of overlapping elements
then the above is simply changed to:
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1;
An example to show the result:
>> nx = 100; nwind = 10; noverlap = 2;
>> idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1
idx =
1 9 17 25 33 41 49 57 65 73 81 89
2 10 18 26 34 42 50 58 66 74 82 90
3 11 19 27 35 43 51 59 67 75 83 91
4 12 20 28 36 44 52 60 68 76 84 92
5 13 21 29 37 45 53 61 69 77 85 93
6 14 22 30 38 46 54 62 70 78 86 94
7 15 23 31 39 47 55 63 71 79 87 95
8 16 24 32 40 48 56 64 72 80 88 96
9 17 25 33 41 49 57 65 73 81 89 97
10 18 26 34 42 50 58 66 74 82 90 98