Comparing values across matrices with a tolerance range - matlab

I have 4 matrices having X,Y,Z co-ordinates of different points.say the matrices are:
A=[ 1 2 5 5; 2 3 4 4; 44 5 65 55]
B=[ 1 3 4 5; 2 3 14 146; 4 5 45 1]
C=[ 2 4 5 6 ; 4 5 6 8; 3 44 5 66]
D=[4 5 6 8; 1 3 4 5; 12 3 4 5]
I want to check whether there are any common point across the four matrices. I have a tolerance of +/- 1 for each of X,Y,Z.
I can run a loop for this but there is the problem of complexity coming up:
when the number of matrices increases the the complexity of the code increases hugely. Any way out regarding this?
Any better procedure regarding the problem?

Your question is a bit unclear, but I am assuming you are looking for indices i,j for which A(i,j)==B(i,j)==C(i,j)==D(i,j), within some tolerance? If so this code will work.
A=[ 1 2 5 5; 2 3 4 4; 44 5 65 55];
B=[ 1 3 4 5; 2 3 14 146; 4 5 45 1];
C=[ 2 4 5 6 ; 4 5 6 8; 3 44 5 66];
D=[4 5 6 8; 1 3 4 5; 12 3 4 5];
% concatenate A,B,C,D into a Nrow x Ncol x Nvars matrix
M = cat(3, A,B,C,D);
% find maximum and minimum along dimension 3
maxVal = max(M, [], 3);
minVal = min(M, [], 3);
% find which row,col indices have a range within 0.00001
valRange = maxVal - minVal;
matchPoints = (valRange <= 0.00001);
% boolean value representing if any match was found
matchFound = any(matchPoints(:));

thank you for your reply.I wrote this function to compare two matrices:
function [comp] = compare( A, B )
[m,n]= size(A);
[mm,nn]=size(B);
k=1;
comp=[];
for ii=1: m
for i=1:mm
if A(ii,1) < (B(i,1)+2) & A(ii,1)> (B(i,1)-2)
if A(ii,2) < (B(i,2)+2) & A(ii,2)> (B(i,2)-2)
if A(ii,3) < (B(i,3)+2) & A(ii,3)> (B(i,3)-2)
comp(k,:)= [A(ii,:), B(i,:)];
k=k+1;
else continue
end
else continue
end
else continue
end
end
end
end
My data is an output of activated co-ordinates of brain across several days. So I want to group regions ( across days) that are nearby each other in a cluster. I tried to attach a picture to explain what I want. But stack overflow is not allowing me.
I hope that this will clarify my post further.

Related

Matlab : How to make label output from interval data?

So i have this data:
A=
2
4
8
9
4
6
1
3
And 3 interval
B=
1 4
5 8
9 12
How to make an output like this
Output=
1
1
2
3
1
2
1
1
The output is based on the interval
you can solve it in several ways. for example, with arrayfun:
A = [2 4 8 9 4 6 1 3].';
B = [1 4;
5 8;
9 12];
res = arrayfun(#(x) find((x >= B(:,1)) & (x <= B(:,2))),A);
If the interval always has the same length, as in your case 4, you can solve it as follows:
Output=ceil(A/4);
If it is not the case, and if not all numbers necessarily fall between any of the intervals, you can compute it as follows. A zero is outputted if a number does not fall within any of the intervals.
% example entry
A=[2 3 4 8 9 4 6 1 3]';
B=[1 4;5 7;9 12]';
Arep=A(:,ones(size(B,2),1)); % replicate array (alternatively use repmat)
Alog=Arep>=B(1,:)&Arep<=B(2,:); % conditional statements, make logical array
Output=Alog*(1:size(B,2))'; % matrix product with natural array to obtain indices

Matlab subscript indices error

help me please. i always get a subscript indices must either be real positive integers or logical error whenever i put 0 value on my "data" how can i get rid of it, i need to have a zero on that one. whenever there is a zero Voltage(1,0) = 1. but I can't get through.
Voltage = [0 1 1 3 4 1; 1 0 5 4 5 3; 6 4 0 4 5 7; 9 3 4 0 6 4; 7 8 5 6 0 7; 4 5 6 7 3 0];
data =[0 2 3 4; 5 6 7 8; 2 3 4 5; 4 5 6 7; 3 4 5 6; 1 3 5 7; 1 2 3 4; 3 4 5 6];
Vm = data(:,1);
Vn = data(:,2);
R = data(:,3);``
X1 = data(:,4);
sz=max(Vn)
y=1:sz
for Vm=data(:,1)
if Vm==0
Voltage(y,Vm)=1
Voltage(y,Vm)=logical(Voltage(y,Vm));
Current = Voltage(y,Vm)-Voltage(y,Vn);
else Vm >= 1
Current = Voltage(y,Vm)-Voltage(y,Vn);
end
end
You are trying to reference a value in the else statement in the Voltage matrix using y but y is not an integer it is an array (or 1d matrix). If you display y you will see that it is 1 2 3 4 5 6. There are several sections of offending code, one of which is:
else Vm >= 1
disp(y) # `y` is not an integer and therefore not a valid index.
Current = Voltage(y,Vm)-Voltage(y,Vn);
To fix it, decide if y should be static or change in the loop.
Let me know if you want a further explanation.

How to align vectors with asynchronous time stamp in matlab?

I would like to align and count vectors with different time stamps to count the corresponding bins.
Let's assume I have 3 matrix from [N,edges] = histcounts in the following structure. The first row represents the edges, so the bins. The second row represents the values. I would like to sum all values with the same bin.
A = [0 1 2 3 4 5;
5 5 6 7 8 5]
B = [1 2 3 4 5 6;
2 5 7 8 5 4]
C = [2 3 4 5 6 7 8;
1 2 6 7 4 3 2]
Now I want to sum all the same bins. My final result should be:
result = [0 1 2 3 4 5 6 7 8;
5 7 12 16 ...]
I could loop over all numbers, but I would like to have it fast.
You can use accumarray:
H = [A B C].'; %//' Concatenate the histograms and make them column vectors
V = [unique(H(:,1)) accumarray(H(:,1)+1, H(:,2))].'; %//' Find unique values and accumulate
V =
0 1 2 3 4 5 6 7 8
5 7 12 16 22 17 8 3 2
Note: The H(:,1)+1 is to force the bin values to be positive, otherwise MATLAB will complain. We still use the actual bins in the output V. To avoid this, as #Daniel says in the comments, use the third output of unique (See: https://stackoverflow.com/a/27783568/2732801):
H = [A B C].'; %//' stupid syntax highlighting :/
[U, ~, IU] = unique(H(:,1));
V = [U accumarray(IU, H(:,2))].';
If you're only doing it with 3 variables as you've shown then there likely aren't going to be any performance hits with looping it.
But if you are really averse to the looping idea, then you can do it using arrayfun.
rng = 0:8;
output = arrayfun(#(x)sum([A(2,A(1,:) == x), B(2,B(1,:) == x), C(2,C(1,:) == x)]), rng);
output = cat(1, rng, output);
output =
0 1 2 3 4 5 6 7 8
5 7 12 16 22 17 8 3 2
This can be beneficial for particularly large A, B, and C variables as there is no copying of data.

How to generate random matrix without repetition in rows and cols?

How to generate random matrix without repetition in rows and cols with specific range
example (3x3): range 1 to 3
2 1 3
3 2 1
1 3 2
example (4x4): range 1 to 4
4 1 3 2
1 3 2 4
3 2 4 1
2 4 1 3
A way of approaching this problem is to generate a circular matrix and shuffle it.
mat_size = 4
A = gallery('circul', 1:mat_size); % circular matrix
B = A( randperm(length(A)) , randperm(length(A)) ); % shuffle rows and columns with randperm
It gives
A =
1 2 3 4
4 1 2 3
3 4 1 2
2 3 4 1
B =
3 4 1 2
2 3 4 1
4 1 2 3
1 2 3 4
This method should be fast. An 11 size problem is computed in 0.047021 seconds.
This algorithm will do the trick, assuming you want to contain all elements between 1 and n
%// Elements to be contained, but no zero allowed
a = [1 2 3 4];
%// all possible permutations and its size
n = numel(a);
%// initialization
output = zeros(1,n);
ii = 1;
while ii <= n;
%// random permuation of input vector
b = a(randperm(n));
%// concatenate with already found values
temp = [output; b];
%// check if the row chosen in this iteration already exists
if ~any( arrayfun(#(x) numel(unique(temp(:,x))) < ii+1, 1:n) )
%// if not, append
output = temp;
%// increase counter
ii = ii+1;
end
end
output = output(2:end,:) %// delete first row with zeros
It definitely won't be the fastest implementation. I would be curios to see others.
The computation time increases exponentially. But everything up to 7x7 is bearable.
I wrote another code (interesting to compare timings and, if possible, to make it parallel). Also had problem with perms (needed to restart Matlab to be able to generate for 11 elements, I have x64 and 16GB of memory). Than I decided to keep characters instead of the numbers, reducing the memory occupied by the matrix. It, of course, generates all permutations, and I shuffle them in the beginning, selecting in the loop in a new random order. It runs faster this way and 'eats' less memory. Time for 11 x 11 (of course it differs from run to run) is shown in results.
clear all;
t = cputime;
sze = 11;
variations = perms(char(1 : sze)); % permutations
varN = length(variations);
variations = variations(randperm(varN)', :); % shuffle
sudoku = zeros(sze, sze);
sudoku(1, :) = variations(1, :); % set the first row
indx = 2;
for ii = 2 : varN
% take a random index
rowVal = variations(ii, :);
% check that row numbers do not present in table at
% corresponding columns
if (~isempty(find(repmat(rowVal, sze, 1) - sudoku == 0, 1)))
continue;
end;
sudoku(indx, :) = rowVal;
disp(['Found row ' num2str(indx)]);
indx = indx + 1;
if indx > sze, break; end;
end;
disp(cputime - t);
disp(sudoku);
Result
252.9712 seconds
7 11 3 9 6 2 4 1 8 10 5
1 9 6 3 10 7 11 5 2 4 8
9 6 11 8 2 10 1 7 4 5 3
4 10 7 11 1 8 5 2 6 3 9
2 5 9 1 3 6 8 4 10 7 11
10 3 5 6 7 4 2 9 11 8 1
6 4 2 10 8 5 3 11 9 1 7
3 8 10 4 11 1 7 6 5 9 2
11 1 8 5 4 9 6 3 7 2 10
5 2 4 7 9 3 10 8 1 11 6
8 7 1 2 5 11 9 10 3 6 4
Here's a memory-efficient approach. The time it takes is random, but not very large. All possible output matrices are equally likely.
This works by randomly filling the matrix until no more positions are available or until the whole matrix has been filled. The code is commented so it should be obvious how it works.
For size 11 this takes of the order of a few thousands or tens of thousands attempts. On my old laptop that means a (random) running time from a few seconds to tens of seconds.
It could perhaps be sped up using uint8 values instead of double. I don't think that brings a large gain, though.
The code:
clear all
n = 11; %// matrix size
[ ii jj ] = ndgrid(1:n); %// rows and columns of S
ii = ii(:);
jj = jj(:);
success = 0; %// ...for now
attempt = 0; %// attempt count (not really needed)
while ~success
attempt = attempt + 1;
S = NaN(n, n); %// initiallize result. NaN means position not filled yet
t = 1; %// number t is being placed within S ...
u = 1; %// ... for the u-th time
mask = true(1, numel(ii)); %// initiallize mask of available positions
while any(mask) %// while there are available positions
available = find(mask); %// find available positions
r = randi(numel(available), 1); %// pick one available position
itu = ii(available(r)); %// row of t, u-th time
jtu = jj(available(r)); %// col of t, u-th time
S(itu, jtu) = t; %// store t at that position
remove = (ii==itu) | (jj==jtu);
mask(remove) = false; %// update mask of positions available for t
u = u+1; %// next u
if u > n %// we are done with number t
t = t+1; %// let's go with new t
u = 1; %// initiallize u
mask = isnan(S(:)); %// initiallize mask for this t
end
if t > n %// we are done with all numbers
success = 1; %// exit outer loop (inner will be exited too)
end
end
end
disp(attempt) %// display number of attempts
disp(S) %// show result
An example result:
10 11 8 9 7 2 3 4 1 6 5
8 4 2 1 10 11 6 5 7 9 3
2 3 5 6 11 8 1 10 4 7 9
9 8 7 4 6 10 11 3 5 1 2
3 5 9 8 2 1 4 7 6 11 10
11 9 4 5 3 6 2 1 8 10 7
1 2 6 3 8 7 5 9 10 4 11
7 1 11 10 5 4 9 8 2 3 6
4 7 1 2 9 3 10 6 11 5 8
6 10 3 11 1 5 7 2 9 8 4
5 6 10 7 4 9 8 11 3 2 1

What is the simplest way to create a weight matrix bases on how frequent each element appear in the matrix?

This is the input matrix
7 9 6
8 7 9
7 6 7
Based on the frequency their appearance in the matrix (Note. these values are for explanation purpose. I didn't pre-calculate them in advance. That why I ask this question)
number frequency
6 2
7 4
8 1
9 2
and the output I expect is
4 2 2
1 4 2
4 2 4
Is there a simple way to do this?
Here's a three-line solution. First prepare the input:
X = [7 9 6;8 7 9;7 6 7];
Now do:
[a m n] = unique(X);
b = hist(X(:),a);
c = reshape(b(n),size(X));
Which gives this value for c:
4 2 2
1 4 2
4 2 4
If you also wanted the frequency matrix, you can get it with this code:
[a b']
Here is a code with for-loop (a is input matrix, freq - frequency matrix with 2 columns):
weight = zeros(size(a));
for k = 1:size(freq,1)
weight(a==freq(k,1)) = freq(k,2);
end
Maybe it can be solved without loops, but my code looks like:
M = [7 9 6 ;
8 7 9 ;
7 6 7 ;];
number = unique(M(:));
frequency = hist(M(:), number)';
map = containers.Map(number, frequency);
[height width] = size(M);
result = zeros(height, width); %allocate place
for i=1:height
for j=1:width
result(i,j) = map(M(i,j));
end
end