consider i have an odd matrix with dimension:
x = <349*385 double>
and then i do this following step:
order = [1 9 2 3 10 17 25 18 11 4 5 12 19 26 33 ...
41 34 27 20 13 6 7 14 21 28 35 42 49 57 50 ...
43 36 29 22 15 8 16 23 30 37 44 51 58 59 52 ...
45 38 31 24 32 39 46 53 60 61 54 47 40 48 55 ...
62 63 56 64];
x = double(x);
y = im2col(x, [8 8],'distinct');
xb = size(y, 2);
y = y(order, :);
y = y(:)';
I get y :
y = <64x2156 double>
xb = 2156;
y = <1x137984 double>
and I transform y into cell :
for i=1:length(x)
r{i} = y(floor((end/length(x))*(i-1)+1):ceil((end/length(x))*i));
end
finally i reverse r into matrix :
y1 = cell2mat(r);
I get y1 :
y1 = <1x138308 double>
My question is why y1 can't get the same result with y? I want y1 and y same. Maybe i wrong when dividing matrix y into cell r. Please help me.
Related
I having trying to run linear regression and plot the results on the below data.
x1 = [40 45 38 50 48 55 53 55 58 40 55 48 45 55 60 60 60 65 50 58];
x2 = [25 20 30 30 28 30 34 36 32 34 38 28 30 36 34 38 42 38 34 38];
y = [1 2 1 3 2 3 3 4 4 3 5 3 3 2 4 5 5 5 4 3];
I have attempted it with this code
X = [ones(size(x1)) x1 x2 (x1.*x2)];
b = regress(y, X);
scatter3(x1,x2,y,'filled')
I am getting this error
Error using regress
Y must be a vector and must have the same number of rows as X.
Both y and X have one row each so I am unsure how to go about this regression.
Any help is appreciated.
Consider the matrix below:
A = randi([0 100], 5, 7)
62 59 20 42 49 41 83
37 34 13 65 82 47 24
93 33 19 93 30 37 20
42 49 74 96 26 41 21
14 85 14 90 77 4 96
first I want to find the maxima of each row and the column index:
83 7
82 5
93 1
96 4
96 7
then I need to find the row with the lowest maxima:
82 5 2
so far, I have implemented this:
close all;
clear all;
clc;
A = randi([0 100], 5, 7);
[M1, I1] = max(A, [], 2);
[M1, I1]
[M2, I2] = min(M1);
[M2, I1(I2), I2]
Though it feels like a workaround, I wondered if there is a more canonical way to do this? Maybe a built-in function or min/max syntax for this specific purpose?
P.S. Shared this question also on the MATLAB Discord channel.
What about this with find?
M = min(max(A,[],2));
[i,j] = find(A == M);
[M,j,i]
lets say
g(:,:,1) = [ 1; 4; 7]
g(:,:,2) = [11; 44; 77]
g(:,:,3) = [111; 444; 777] .
Lets say a = [2; 3; 1] and b = [1; 3; 2]. I want the output like this
[4;777;11]. first element is g(2,:,1) , second element is g(3,:,3) and third element is g(1,:,2).
It's as simple as this -
[m,n,r] = size(g)
out = g(a + (b-1)*m*n)
For a generic case, when you want to specify the column number as well -
out = g(a + (col_num-1)*m + (b-1)*m*n)
For a more generic case, when you want to specify more than just one column -
g(bsxfun(#plus,(col_nums-1)*m,a(:)+(b(:)-1)*m*n))
For even more generic cases, you have to ask harder questions.
Sample run -
>> g
g(:,:,1) =
11 81 26 19 87
96 87 80 27 58
1 9 43 15 55
77 40 91 14 15
g(:,:,2) =
85 40 19 90 34
62 8 24 94 90
35 24 42 49 37
51 13 5 49 12
g(:,:,3) =
78 10 57 82 65
39 14 6 2 73
24 94 24 5 65
40 95 35 17 45
>> [m,n,r] = size(g);
>> a = [2,3,1]; b = [1,3,2];
>> col_nums = [1 3];
>> g(bsxfun(#plus,(col_nums-1)*m,a(:)+(b(:)-1)*m*n))
ans =
96 80
24 24
85 19
assume I have a 10x10 matrix M
M=[64 36 50 87 22 45 37 23 68 88;
33 23 87 49 54 25 35 98 78 52;
12 54 76 43 24 87 54 98 45 34;
77 87 23 45 34 65 23 76 12 76;
12 34 55 44 76 98 93 23 54 67;
22 55 78 90 88 56 34 23 12 76;
99 23 67 89 34 23 12 87 45 23;
22 54 76 89 65 23 45 12 93 12;
44 56 23 88 67 14 15 67 34 12;
11 44 77 99 34 23 78 34 12 79];
I want to first find out the local maximum in the matrix
and then according to the maximum position do a sum over a 3x3 region over M
For the first step, the code I used is local_max=imregionalmax(M). to find out the local maximum position, but how can I go further to use this coordination to sum over a 3x3 matrix over M?
Thanks for the help.
You can calculate the sum for the whole matrix and then only keep the values that you're interested in. This should work:
local_max=imregionalmax(M)
sums = imfilter(M, ones(3));
local_max_sums = sums(local_max);
And if what you want is a matrix with non-zero entries where the local maxima are located:
local_max_sums = sums .* local_max;
You seem to be looking for the matrix subset functionality of Matlab.
Basically, for
M = [ 1 2 3 4 5 6;
4 5 6 7 8 9;
7 8 9 0 1 2;
0 1 2 3 4 5;
3 4 5 6 7 8;
6 7 8 9 0 1];
If you have a max at (3,3), you can use M(2:4, 2:4) to get
N = [ 5 6 7;
8 9 0;
1 2 3];
Summing that matrix is all that remains - as simple as
total = sum(N(:));
This is kind of brute force for Matlab, but I think it works.
bw = imregionalmax(M);
[x,y] = find(bw);
s = [];
for i = 1:length(x)
startX = x(i)-2;
if(startX < 1)
startX = 1;
end
endX = x(i)+2;
if endX > 10
endX = 10;
end
startY = y(i)-2;
if startY < 1
startY = 1;
end
endY = y(i)+2;
if endY > 10
endY = 10;
end
s(i) = sum2(M(startX:endX, startY:endY));
end
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