Returning five minimum integers from a vector consists of twenty integers in MATLAB [duplicate] - arrays

This question already has answers here:
How to find the index of the n smallest elements in a vector
(2 answers)
Closed 7 years ago.
I would like to return five minimum integers from a vector consists of twenty integers in MATLAB. Any help? Thanks.
Example:
X = [6 7 8 3 5 6 7 2 5 1 0 6 6 2 9 6 3 3 4 77];
How to get the five minimum values from this vector?

You can use Sort function and then take the 5 in the end of the array
sorted_x = sort(X)
5Minimum = sorted_x(15:20)

Related

Ordering an array with the smallest value first MATLAB [duplicate]

This question already has answers here:
Sorting entire matrix according to one column in matlab
(2 answers)
Closed 4 years ago.
I'm trying to get the smallest value in the 2D array but keep the order of the 2D array, for example:
If I had the array
7 | 7
2 | 3
8 | 0
3 | 7
5 | 5
I want to order it so it displays
2 | 3
8 | 0
3 | 7
5 | 5
7 | 7
I've looked at mink and sortrows but none seem to give the output I need.
Any help with this would be much appreciated!
You can use circshift to change the order of the elements in the way you need. First find the index to the minimal element, then shift so that the element is at the top:
M = [7,7 % example data from OP
2,3
8,0
3,7
5,5];
[~,ind] = min(M(:,1));
M = circshift(M,1-ind,1);
For the updated question, you first need to identify the row that you want to be moved to the top of the matrix. Once you know this you can simply rearrange the matrix so that this row, and all that occur after it, are placed first.
A = [7 7
2 3
8 0
3 7
5 5]
[~,index] = min(A(:,1)); % Identify row which should occur first
A = A([index:end,1:(index-1)], :) % Rearrange rows
A =
2 3
8 0
3 7
5 5
7 7

Matlab ,How to get elements in a matrix using two arrays [duplicate]

This question already has answers here:
Construct a matrix from a set of coordinates
(5 answers)
Closed 6 years ago.
For example, I have a matrix like A=[1,2,3,4;5,6,7,8;9,10,11,12]. And two array x=[1,1,3,2] and y = [2,4,3,1], which represent X- and Y-coordinate.
And I want to get 4 elements in the matrix [A(1,2);A(1,4);A(3,3);A(2,1)]. I use this code: result = diag(A(x,y)); Although I get what I want, but if I deal with a large matrix, such code runs too slow for me. Dose someone have a better way?
thanks!
Probably not the faster ones, but following are some approaches to this:
A = [1 2 3 4;
5 6 7 8;
9 10 11 12];
x = [1 1 3 2];
y = [2 4 3 1];
%Approach-1 (Yours approach)
diagonal = diag(A(x,y))
%Approach-2
A1=A(x,y); LowUp=A1(tril(triu(A1))~=0)
%Approach-3
EYE= A1((eye(4,4).*A1)~=0)
%Approach-4
findeye=A1(find(eye(size(A1))))
%Approach-5
subind=A(sub2ind(size(A),x,y)).'
%Approach-6
for i=1:4
loop(i)=A(x(i),y(i));
end
loop=loop.'
You need sub2ind
A = [1,2,3,4;
5,6,7,8;
9,10,11,12];
x = [1,1,3,2];
y = [2,4,3,1];
id = sub2ind(size(A),x,y)
id =
4 10 9 2
A(id)
ans =
2 4 11 5

Insert zeros into vector [duplicate]

This question already has answers here:
Inserting variable number of zeros between non-zero elements in a vector in MATLAB
(3 answers)
Closed 7 years ago.
How do I insert 3 zeros in vector a:
a = [1 2 3 4 5 6 7 8 9]
such that vector v is obtained:
v = [1 2 3 0 0 0 4 5 6 0 0 0 7 8 9]
it should be automated such that it can be implemented in a vector with length n.
assuming v does not exist and numel(a) is a multiple of n:
a = 1:9;
n = 3;
v(bsxfun(#plus, (1:n)', 0:2*n:(numel(a)-n)*2)) = a

Build a large array from a small array in MATLAB? [duplicate]

This question already has answers here:
Octave / Matlab: Extend a vector making it repeat itself?
(3 answers)
Closed 8 years ago.
If I have a small array like a=[1 2 3 4 5], and want to build a large array from it with repeating it, like b=[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ....1 2 3 4 5], how can I do that in simplest way and lowest calculations?
repmat is what you are looking for
n = 5
b = repmat(a,1,n)

MATLAB: doubling the occurrences of a value [duplicate]

This question already has an answer here:
MATLAB: Duplicate each element of a vector? [closed]
(1 answer)
Closed 8 years ago.
It's hard to explain so I will show an example of what I would like to do:
x = [1 2 3 4 5]
I would like the outcome to be:
x = [1 1 2 2 3 3 4 4 5 5]
Preferably without the use of a for loop, but either method would be appreciative.
Thanks.
You can also use the Kronecker tensor product (kron function) which is pretty neat:
x = kron(x,ones(1,2))
x =
1 1 2 2 3 3 4 4 5 5
If you want it sorted as you have here, you could do:
y = sort([x x]);
alternatively if the order matters:
y = reshape([x;x],[1,2*length(x)])

Resources