obtain data using columns data indices - matlab

i have data A=( 3,5,3,1,4 ) in a column and
B=[
4 6 9 1 3
2 7 2 5 7
7 3 1 8 2
4 1 6 9 1
2 5 8 3 6 ]
And i want: as in A first element is 3 and for this i want to get first element of column 3 row 1 from B which is 9. The second element of A is 5 and for this i want to get the the 2nd element of column 5 and row 2 from B which is 7 ,and do the process for all other elements . how to do this in matlab? the required elements are bold and underlined. The desired output is [9,7,1,4,3]

Read about linear indexes.
sub2ind will convert from [row col] to index.
Cols=[ 3,5,3,1,4 ];
Rows=1:length(Cols);
B=[
4 6 9 1 3
2 7 2 5 7
7 3 1 8 2
4 1 6 9 1
2 5 8 3 6 ];
Indexes=sub2ind(size(B),Rows,Cols);
Vals=B(Indexes)

if I've read well you want an elements replacement. It's quite simple
A(1)=B(1,3)
A(2)=B(2,5)
So after you have declared your two vectors you can handle them replacing singular components. In general when you have a 1-D vectors you can access to it's component position only declaring the position itself inside the parentheses, as I did for A.
Whe you have to face situation like in B, If you remember linear algebra and matrix in general B(a,b) means the element of matrix B placed in the line a and column b so you have to specify row and column to access that element.

Related

Is there any command to find mean of first 5 values then next 5 values for a total of 1000 values in a vector in MATLAB

Is there any command to find mean of first 5 values then next 5 values from a total of 25 values present in a vector in MATLAB. If the dataset is X.
If anyone can help me to provide a code where I can get mean at every 5th value.
X=[4 5 6 7 2 5 7 4 2 6 7 3 2 1 5 7 8 3 4 6 8 4 2 6 8];
You can for instance reshape the vector in an array with reshape and then apply the mean function:
M = mean(reshape(X, [5, numel(X)/5]),1);
or simply
M = mean(reshape(X, 5, []),1);
But there as stated in the comments there are many other ways.
Here is one simple way to do it. Rearrange the vector into a matrix loop over the columns and take the mean of all values in each column. Store the results in a new vector.
X=[4 5 6 7 2 5 7 4 2 6 7 3 2 1 5 7 8 3 4 6 8 4 2 6 8];
Xr = reshape(X,5,5)
cols = size(Xr)(2)
avgs=zeros(1,cols)
for i= 1:cols
avgs(i) = mean(Xr(:,i))
end

Reshape a matrix by splitting it after k columns in MATLAB

Suppose that I have a matrix , let's call it A, as follows:
1 2 3 4 5 1 2 3 4 5
0 2 4 6 8 1 3 5 7 9
And I want to reshape it into a matrix like this:
1 2 3 4 5
0 2 4 6 8
1 2 3 4 5
1 3 5 7 9
So, basically, what I want to be done is that MATLAB first reads a block of size (2,5) and then splits the remaining matrix to the next row and then repeats this so on so forth until we get something like in my example.
I tried to do this using MATLAB's reshape command in several ways but I failed. Any help is appreciated. In case that it matters, my original data is larger. It's (2,1080). Thanks.
I don't believe you can do this in a single command, but perhaps someone will correct me. If speed isn't a huge concern a for loop should work fine.
Alternatively you can get your results by reshaping each row of A and then placing the results into every other row of a new matrix. This will also work with your larger data.
A = [1 2 3 4 5 1 2 3 4 5
0 2 4 6 8 1 3 5 7 9];
An = zeros(numel(A)/5, 5); % Set up new, empty matrix
An(1:2:end,:) = reshape(A(1,:), 5, [])'; % Write the first row of A to every other row of An
An(2:2:end,:) = reshape(A(2,:), 5, [])' % Write second row of A to remaining rows
An =
1 2 3 4 5
0 2 4 6 8
1 2 3 4 5
1 3 5 7 9
You may need to read more about indexing in the Matlab's documentation.
For your example, it is easy to do the following
A=[1 2 3 4 5 1 2 3 4 5; 0 2 4 6 8 1 3 5 7 9]
a1=A(:,1:5); % extract all rows, and columns from 1 to 5
a2=A(:,6:end); % extract all rows, and columns from 6 to end
B=[a1;a2] % construct a new matrix.
It is not difficult to build some sort of loops to extract the rest.
Here's a way you can do it in one line using the reshape and permute commands:
B = reshape(permute(reshape(A,2,5,[]), [1,3,2]), [], 5);
The reshape(A,2,5,[]) command reshapes your A matrix into a three-dimensional tensor of dimension 2 x 5 x nblocks, where nblocks is the number of blocks in A in the horizontal direction. The permute command then swaps the 2nd and 3rd dimensions of this 3D tensor, so that it becomes a 2 x nblocks x 5 tensor. The final reshape command then transforms the 3D tensor into a matrix of dimension (2*nblocks) x 5.
Looking at the results at each stage may give you a better idea of what's happening.

How can I perfect out my zigzag matlab function?

I was given the following Question:
Write a function call zigzag that takes in a 2-dimensional array A and return a 1- dimensional array created by traverse through A in zigzag way starting at position (1,1).
Example:
A =[1 2 3 4 5 6
7 8 9 1 3 4
3 4 5 6 3 1
3 4 5 6 7 8]
zigzag(A) should return:
[1 2 3 4 5 6 4 3 1 9 8 7 3 4 5 6 3 1 8 7 6 5 4 3]
The way I solved it, I am not sure if this is a correct method to do it. I would be glad to know if this is perfect and how I could improve my answer:
function B=zigzag(A)
[r,c]=size(A);
B= reshape(A’,1,:);
m=0
n=0
For r>m+2
m=m+2;
n=n+1;
For i=1:c
B(nc+i)=B(2cn-i+1);
End
End
disp(B)
If it gives you the right output, then you're certainly doing something right. However, what I would have done was access the even rows of your matrix, reverse the directions so that they're displayed in reverse order, transpose your matrix then unravel it.
The reason why we transpose it is because when we unravel a matrix in MATLAB, this means that the columns of the matrix are stacked on top of each other so that one single vector is produced. We want the rows to be stacked on top of each other and making the even rows in reverse order will allow you to do the zigzag that you expect. If you want the rows to be stacked on top of each other, you need to transpose the matrix first so that rows become columns, and when you unravel this matrix, you'll stack the rows on top of each other instead to create a single vector.
Something like this:
B = A; %// Make a copy
B(2:2:end,:) = fliplr(B(2:2:end,:)); %// Flip even rows
B = reshape(B.', 1, []); %// Unravel
With your example, I get:
B =
Columns 1 through 13
1 2 3 4 5 6 4 3 1 9 8 7 3
Columns 14 through 24
4 5 6 3 1 8 7 6 5 4 3

Sorting entire matrix according to one column in matlab

I have the matrix as follows
a =
1 3
2 5
3 2
4 8
5 9
I want to sort the second column in the a matrix. I want the corresponding rows of column one to be printed as follows :
a =
3 2
1 3
2 5
4 8
5 9
I tried sort(a), but it is sorting only the second column of matrix a.
Try this:
sortrows(a,2)
This should sort according to the second column.
or use:
[val idx]=sort(a(:,2));
ans = [a(idx,1) val]

Get first two maximum values in matrix

I've got a matrix (n x m). And I'd like to know, for each row, the indexes of the coloums that contain the first two maximum values:
2 3 4 2
2 4 7 1
1 1 2 4
5 5 9 6
1 4 2 1
9 8 1 2
The answer should be:
2 3
2 3
3 4
3 4
2 3
1 2
How can I obtain it with matlab commands? I'd like not to use for loops. I tried with:
[x,y]=max(matrix')
y=y';
y gives me the colum indexes for the maximum elements. Now I'd set to zero these elements and repeat the instructions but I have no idea how to do. I treid:
matrix(:,y)=0;
but it doesn't work.
if A is your matrix, then sort and pick the top two indices,
[a ix]=sort(A,2)
ans= ix(:,end-1:end)