How to Convert Vector to String in Matlab - matlab

I'm hoping to find a simple way of converting a vector of numbers to a string in matlab.
Lets say I have a vector b,
b[1 2 4 3];
is there something simple such as
b = vect2str(b);

how about
b = [1 2 4 3];
b = num2str(b);

Related

Extracting a vector from a matrix using vector of column indices

I have a matrix
A = [ 5 2
2 3
-4 7 ];
and a vector v = [1 2 1]. I want extract the vector B from A using the columns indexed by v. That is, B should look like
B = [ 5
3
-4 ];
I tried B = A(:,v) but that didn't work. Any simple way to do this?
You have the column subscripts. Generate the row subscripts and use sub2ind to get linear indices to retrieve desired elements of A.
B = A(sub2ind(size(A),1:numel(v),v))

Using vector as indices not working as expected, matlab

Given a nXm matrix A and a mX2 matrix B and a matrix C of size mX1 containing 1s and 2s C=[1 2 1 2 1...], depending on which column, I want every row of A to be multiplied with. How can this be done? Or equivalently, given D = A*B how can I access only the values dictated by C. I tried D(:,C), but the result is not the expected.
Example a =[1 2; 3 4; 5 6] . c = [1 2 1] . a(?) = [1 4 5]
Any idea?
%example data
n=10;m=20;
A=rand(n,m)
B=rand(m,2)
C=round(rand(m,1))+1;
%solution:
B2=B(:,1); %multiplication vector
B2(C==2)=B(C==2,2) %change the ones where C==2
A*B2
You can run the following command for the last example:
a(sub2ind([3,2],1:3,c))'
In general case you can do like the following:
% n is the length of the D which is nx2 matrix
D(sub2ind([n,2],1:n,C))'

Insert embeded number in a vector matlab

Hey everyone I would like to do the following. I have a vector f.e. [1 2 3 4 6 8] and I want to end up in that vector [1 2 3 4 5 6 7 8] but generally, not like [v(1:4) 5 v(6) 7 v(8)].
Thank you very much!!
If you know your vector is going to be sorted you can use:
a = [1 2 3 4 6 8]; then
a = sort([a,5,7]);
This appends the additional values to the vector, sorts them, and assigns the sorted vector to the original variable.
Since the question is too vague, I'm not sure that I understand correctly, but this is what I came up with
If you have array to be modified
a = [a_1, ..., a_n]
and you want to insert
b = [b_1, ..., b_m]
and the position you want to insert
b_pos = [p_1, ..., p_m]
Then
n = length(a);
m = length(b);
a_pos = setdiff(1:(n+m),b_pos) % find index which is not included in b_pos
c = zeros(1,n+m);
c(a_pos) = a;
c(b_pos) = b;

compare multiple matrices matlab

I have multiple matrices of the same size and want to compare them.
As a result I need a matrix which gives me the biggest of the 3 for every value.
I will clarify what i mean with an example:
I have 3 matrices with data of 3 persons.
I would like to compare these 3 and get a matrix as result.
In that matrix every cell/value should be the name of the matrix who had the highest value for that cell. So if in the 3 matrices the first value (1 colum, 1 row) is accordingly 2, 5, 8 the first value of the result matrix should be 3 (or the name of the 3 matrix).
If the three matrices are A, B, C, do this:
[~, M] = max(cat(3,A,B,C),[],3);
It creates a 3D "matrix" and maximizes across the third dimension.
Concatenate them on the 3rd dimension, and the use the SECOND output from max to get exactly what you want
A = rand(3,3);
B = rand(3,3);
C = rand(3,3);
D = cat(3, A, B, C)
[~, Solution] = max(D, [], 3)
e.g.:
D =
ans(:,:,1) =
0.70101 0.31706 0.83874
0.89421 0.33783 0.55681
0.68520 0.11697 0.45631
ans(:,:,2) =
0.268715 0.213200 0.124450
0.869847 0.999649 0.153353
0.345447 0.023523 0.338099
ans(:,:,3) =
0.216665 0.297900 0.604734
0.103340 0.767206 0.660668
0.127052 0.430861 0.021584
Solution =
1 1 1
1 2 3
1 3 1
edit
As I did not know about the second argument of the max-function, here is what you should NOT use:
old
Well, quick&dirty:
x=[2 5 8];
w=max(x)
[~,loc] = ismember(w,x)

find the nearest point pairs between two sets of of matrix

Assume I have two sets of matrix (A and B), inside each matrix contains few point coordinates, I want to find out point in B nearest to A and output a cell array C listed the nearest point pair coordinates accordingly and one cell array D register the unpaired spot, how should I do it?
To be more specific, here is what I want
Two sets of matrix contain spot xy coordinates;
A=[ 1 2; 3 4]; B=[1 3; 5 6; 2 1];
want to get C{1,1}=[1 2; 1 3]; C{2,1}= [3 4; 5 6]; D{1,1}=[2 1];
Thanks for the help.
There is not exactly one solution to this problem, take for example the (one-dimensional, but expandable to N-D) case:
A= [1; 3];
B= [2];
Then either A(1) or A(2) can be the leftover point. Which one your algorithm spits out, will depend on how it works, ie which point you take first to find the nearest point.
Such algorithm consists imo of
Finding distances between each combination of A(i) and B(j). If you have the statistics toolbox, pdist2 does this for you:
A=[ 1 2; 3 4];
B=[1 3; 5 6; 2 1];
dist = pdist2(A,B);
Looping over the smallest of A or B (I'll take A, cause it is smallest in your example) and finding for each point in A the closest point in the remaining set of B:
N = size(A,1);
matchAtoB=NaN(N,1);
for ii=1:N
dist(:,matchAtoB(1:ii-1))=Inf; % make sure that already picked points of B are not eligible to be new closest point
[~,matchAtoB(ii)]=min(dist(ii,:));
end
matchBtoA = NaN(size(B,1),1);
matchBtoA(matchAtoB)=1:N;
remaining_indices = find(isnan(matchBtoA));
Combine result to your desired output matrices C and D:
C=arrayfun(#(ii) [A(ii,:) ; B(matchAtoB(ii),:)],1:N,'uni',false);
D=mat2cell(B(remaining_indices,:),ones(numel(remaining_indices),1),size(B,2));
Note that this code will also work with 1D points or higher (N-D), the pdist2 flattens everything out to scalar distances.
Here's my take on the problem:
A=[1 2
3 4];
B=[1 3
5 6
2 1];
dists = pdist2(A,B);
[dists, I] = sort(dists,2);
c = NaN(size(A,1),1);
for ii = 1:size(A,1)
newC = find(~any(bsxfun(#eq, I(ii,:), c), 1));
c(ii) = I(ii,newC(1));
end
C = cellfun(#(x)reshape(x,2,2).',...
mat2cell([A B(c,:)], ones(size(A,1),1), 4), 'uni', false);
D = {B(setdiff(1:size(B,1),c), :)}
This solution assumes
all your vectors are 2D
stacked in rows of A and B
and A is always the source (i.e., everything is compared to A)
If these assumptions do not (always) hold, you'll have to take a more general approach, like the one suggested by #GuntherStruyf.