swapping two vector's elements in matlab - arrays

I have two vectors, v1 (1x9) and v2 (1x4).
I have to write a function with two inputs, (x,y) and output of out.
x should be the index number x in v1.
y should be the index number y in v2.
The function should replace the xth element in v2 with the ythelement in v1 and give the new v2 as output.
As an example:
v1=[1 2 3 4 5 6 7 8 9];
v2=[1 2 3 4];
out = myfun(7,2);
then the output should be
out = [1 7 3 4];
The next time if x=9 and y=1
out = myfun(9,1);
out = [9 7 3 4];
my main program idea is
[M Z]=test(A,q)
A is matrix (mxn)
q is vector of length m
i=[1:m];j=[1:n];
c(:,j)=q./A(:,j);
find minimum element in c . as example it is c(I,j) then
x=I; and y=j;g
i = [1:x-1,x+1:m]; j = [1:y-1,y+1:n]; % starting new calculations
statements…calculation procedures to find matrix M instead of A and vector Z instead of q.
end of program
now I want to write myfunction which you did before inside this program
elseif v(x) <= v1(n) this program must working continuously (holding M and Z as new A and q in input and get the minimum ratio and so the new x and y ) until v(x)==n break
please help

You can save the variable and load it in the next call.
function v2 = myfun(x,y)
v1=[1 2 3 4 5 6 7 8 9];
v2=[1 2 3 4];
if exist('v2.mat', 'file' ) ~= 0
load('v2')
end
v2(y) = v1(x);
save('v2','v2');
end
You could also use persistant variable,
function out = myfun(x,y)
persistent v
if isempty(v)
v = [1 2 3 4];
end
v1=[1 2 3 4 5 6 7 8 9];
v(y) = v1(x);
out = v;
end
Note
mlock command locks the currently running function in memory so that subsequent clear functions do not remove it. Locking a function in memory also prevents any persistent variables defined in the file from getting reinitialized.
Edit
I got this form your comments, see how it goes,
function out = test(x,y)
persistent v
if isempty(v)
v = [1 2 3 4];
end
v1 = [1 2 3 4 5 6 7 8 9];
v(y) = v1(x);
if x == 3;
out = v;
return
elseif v(y) <= v1(9)
statements
end
out = v;
end

Related

Finding multiple coincidences between two vectors

I have two vectors, and I'm trying to find ALL coincidences of one on the other within a certain tolerance without using a for loop.
By tolerance I mean for example if I have the number 3, with tolerance 2, I will want to keep values within 3±2, so (1,2,3,4,5).
A = [5 3 4 2]; B = [2 4 4 4 6 8];
I want to obtain a cell array containing on each cell the numbers of all the coincidences with a tolerance of 1 (or more) units. (A = B +- 1)
I have a solution with zero units (A = B), which would look something like this:
tol = 0;
[tf, ia] = ismembertol(B,A,tol,'DataScale',1); % For tol = 0, this is equivalent to using ismember
idx = 1:numel(B);
ib = accumarray(nonzeros(ia), idx(tf), [], #(x){x}) % This gives the cell array
The output is:
ib =
[]
[]
[2 3 4]
[1]
Which is as desired.
If I change the tolerance to 1, the code doesn't work as intended. It outputs instead:
tol = 1
[tf, ia] = ismembertol(B,A,tol,'DataScale',1); % For tolerance = 1, this is equivalent to using ismember
idx = 1:numel(B);
ib = accumarray(nonzeros(ia), idx(tf), [], #(x){x}) % This gives the cell array
ib =
[5]
[2 3 4]
[]
[1]
When I would expect to obtain:
ib =
[2 3 4 5]
[1 2 3 4]
[2 3 4]
[1]
What am I doing wrong? Is there an alternative solution?
Your problem is that, in the current state of your code, ismembertol only outputs 1 index per element of B found in A, so you lose information in the cases where an element can be found several times within tolerance.
As per the documentation You can use the 'OutputAllIndices',true value pair argument syntax, to output what you want in ia with just a call to ismembertol:
A = [5 3 4 2]; B = [2 4 4 4 6 8];
tol = 0;
[tf, ia] = ismembertol(A,B,tol,'DataScale',1,'OutputAllIndices',true);
celldisp(ia) % tol = 0
ia{1} =
0
ia{2} =
0
ia{3} =
2
3
4
ia{4} =
1
celldisp(ia) % tol = 1
ia{1} =
2
3
4
5
ia{2} =
1
2
3
4
ia{3} =
2
3
4
ia{4} =
1
Here is a manual approach, just to provide another method. It computes an intermediate matrix of all absolute differences (using implicit expansion), and from the row and column indices of the entries that are less than the tolerance it builds the result:
A = [5 3 4 2];
B = [2 4 4 4 6 8];
tol = 1;
[ii, jj] = find(abs(A(:).'-B(:))<=tol);
ib = accumarray(jj, ii, [numel(A) 1], #(x){x});
Note that this approach
may be memory-intensive, because of the intermediate matrix;
can be made to work in old Matlab versions, because it doesn't use ismembertol; but then implicit expansion has to be replaced by explicitly calling bsxfun:
[ii, jj] = find(abs(bsxfun(#minus, A(:).', B(:)))<=tol);
ib = accumarray(jj, ii, [numel(A) 1], #(x){x});

Create indexing array with 'end' before vector exists

I was just wondering if there is a way to index using end before knowing a vector's size? It should work for arrays with different sizes. Like this:
subvector = (2:end) % illegal use of end
A=[1 2 3];
B=[4 5 6 7];
A(subvector) % should be 2 3
B(subvector) % should be 5 6 7
You can set up an anonymous function to act in a similar way
f_end = #(v) v(2:end);
A = [1 2 3];
B = [4 5 6 7];
f_end( A ); % = [2 3];
f_end( B ); % = [5 6 7];
I think this is the only way you could do it, since you can't set up an indexing array without knowing the end index.
Without indexing or usage of end, one can remove the first element:
f_end = A;
f_end[1] = [];
As a function:
function x = f_end(y, n)
x = y;
x[1:n]=[]; % deletes the first n elements

Pairs of random numbers Matlab

I am trying to generate random numbers between 1 and 6 using Matlab's randperm and calling randperm = 6.
Each time this gives me a different array let's say for example:
x = randperm(6)
x = [3 2 4 1 5 6]
I was wondering if it was possible to create pairs of random numbers such that you end up with x like:
x = [3 4 1 2 5 6]
I need the vector to be arranged such that 1 and 2 are always next to each other, 3 and 4 next to each other and 5 and 6 next to each other. As I'm doing something in Psychtoolbox and this order is important.
Is it possible to have "blocks" of random order? I can't figure out how to do it.
Thanks
x=1:block:t ; %Numbers
req = bsxfun(#plus, x(randperm(t/block)),(0:block-1).'); %generating random blocks of #
%or req=x(randperm(t/block))+(0:block-1).' ; if you have MATLAB R2016b or later
req=req(:); %reshape
where,
t = total numbers
block = numbers in one block
%Sample run with t=12 and block=3
>> req.'
ans =
10 11 12 4 5 6 1 2 3 7 8 9
Edit:
If you also want the numbers within each block in random order, add the following 3 lines before the last line of above code:
[~, idx] = sort(rand(block,t/block)); %generating indices for shuffling
idx=bsxfun(#plus,idx,0:block:(t/block-1)*block); %shuffled linear indices
req=req(idx); %shuffled matrix
%Sample run with t=12 and block=3
req.'
ans =
9 8 7 2 3 1 12 10 11 5 6 4
I can see a simple 3 step process to get your desired output:
Produce 2*randperm(3)
Double up the values
Add randperm(2)-2 (randomly ordered pair of (-1,0)) to each pair.
In code:
x = randperm(3)
y = 2*x([1 1 2 2 3 3])
z = y + ([randperm(2),randperm(2),randperm(2)]-2)
with result
x = 3 1 2
y = 6 6 2 2 4 4
z = 6 5 2 1 3 4

Find index of smallest element in an array not in another array (Matlab)

I have an array a = [6 8 2 1 9] and b = [1 2 6]. I want a function which returns 2 since a(2)=8 which is the smallest element of a not in b.
What I have tried so far:
[A,I] = sort(a);
index = I(find(~ismember(A,b),1))
I would like something faster as this piece of code has to run many times and the arrays involved are very large.
Thanks in advance!
Another (faster, I believe) solution would be:
a = [6 8 2 1 9];
b = [1 2 6];
[d,I] = setdiff(a,b); % d is the set difference, I is the indices of d elements in a
[m,J] = min(d); % m is the minimum in d, J is it's index
I(J) % This is the index of m in d (the value that you want)
ans =
2
Does this do what you need?
>> a = [6 8 2 1 9];
>> b = [1 2 6];
>> min(a(~ismember(a,b)))
ans =
8
Edit:
Oops - I meant
>> find(a==min(a(~ismember(a,b))),1)
ans =
2
This finds the index as you requested, rather than the value, which the first answer gives.

Create all possible Mx1 vectors from an Nx1 vector in MATLAB

I am trying to create all possible 1xM vectors (word) from a 1xN vector (alphabet) in MATLAB. N is > M. For example, I want to create all possible 2x1 "words" from a 4x1 "alphabet" alphabet = [1 2 3 4];
I expect a result like:
[1 1]
[1 2]
[1 3]
[1 4]
[2 1]
[2 2]
...
I want to make M an input to my routine and I do not know it beforehand. Otherwise, I could easily do this using nested for-loops. Anyway to do this?
Try
[d1 d2] = ndgrid(alphabet);
[d2(:) d1(:)]
To parameterize on M:
d = cell(M, 1);
[d{:}] = ndgrid(alphabet);
for i = 1:M
d{i} = d{i}(:);
end
[d{end:-1:1}]
In general, and in languages that don't have ndgrid in their library, the way to parameterize for-loop nesting is using recursion.
[result] = function cartesian(alphabet, M)
if M <= 1
result = alphabet;
else
recursed = cartesian(alphabet, M-1)
N = size(recursed,1);
result = zeros(M, N * numel(alphabet));
for i=1:numel(alphabet)
result(1,1+(i-1)*N:i*N) = alphabet(i);
result(2:M,1+(i-1)*N:i*N) = recursed; % in MATLAB, this line can be vectorized with repmat... but in MATLAB you'd use ndgrid anyway
end
end
end
To get all k-letter combinations from an arbitrary alphabet, use
n = length(alphabet);
aux = dec2base(0:n^k-1,n)
aux2 = aux-'A';
ind = aux2<0;
aux2(ind) = aux(ind)-'0'
aux2(~ind) = aux2(~ind)+10;
words = alphabet(aux2+1)
The alphabet may consist of up to 36 elements (as per dec2base). Those elements may be numbers or characters.
How this works:
The numbers 0, 1, ... , n^k-1 when expressed in base n give all groups of k numbers taken from 0,...,n-1. dec2base does the conversion to base n, but gives the result in form of strings, so need to convert to the corresponding number (that's part with aux and aux2). We then add 1 to make the numbers 1,..., n. Finally, we index alphabet with that to use the real letters of numbers of the alphabet.
Example with letters:
>> alphabet = 'abc';
>> k = 2;
>> words
words =
aa
ab
ac
ba
bb
bc
ca
cb
cc
Example with numbers:
>> alphabet = [1 3 5 7];
>> k = 2;
>> words
words =
1 1
1 3
1 5
1 7
3 1
3 3
3 5
3 7
5 1
5 3
5 5
5 7
7 1
7 3
7 5
7 7
use ndgrid function in Matlab
[a,b] = ndgrid(alphabet)

Resources