To subtract a vector of values in matlab - matlab

Please is there a matlab code to subtract two vector values where one value is always the larger?
Eg;
A=[10 9 8 7 6 5 4 3 2 1]; B=[5 4 3 2 1 10 9 8 7 6];
I want to subtract these two vectors where the minuend is always the larger so that the answer will be:
[5 5 5 5 5 5 5 5 5 5]
How can I do this?

Use the following code: abs(A-B)

Related

Sorting two columns of a matrix while keeping one intact in OCTAVE/MATLAB

I have this matrix:
data=[1 5402783 1
2 43359352 2
3 26118700 3
4 33091887 4
5 890931 5
6 826897 6
7 1188749 7
8 1239861 8];
I need the first column to stay as it is, sort the 2nd column (in descending order) and 'keep along' the values of the third column. If I use sort(data) it sorts all 3 columns.
I tried:
[~,idx]=sort(data(:,2),'descend');
data=data(idx,:)
but it is obviously wrong.
The output should be:
[1 43359352 2
2 33091887 4
3 26118700 3
4 5402783 1
5 1239861 8
6 1188749 7
7 890931 5
8 826897 6]
All you need to do is reassemble the data matrix in the end taking the unsorted and sorted parts:
data = [1 5402783 1
2 43359352 2
3 26118700 3
4 33091887 4
5 890931 5
6 826897 6
7 1188749 7
8 1239861 8];
[~,idx] = sort(data(:,2),'descend');
data = [data(:,1),data(idx,2:3)];

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

How to create a random 3D matrix?

Is there any way of creating a 3D matrix randomly? There are ways to create random 2D matrices using randint function. Is there any inbuilt function like that?
E.g. a 4x4 matrix can be generated easily by using the randint function. What if I want to create a matrix of dimension 4x4x3?
You can use randi(imax, size1, size2, size3) function where imax refers to maximum of random integer values (mean upper bound) and 1 is lower bound. You can expand size argument to sizeN what you want.
This is an example of its usage:
>> A = randi(5, 4, 4, 3)
A(:,:,1) =
4 4 5 4
4 1 2 2
2 1 3 3
4 3 2 4
A(:,:,2) =
5 1 5 1
5 2 2 2
3 5 5 4
1 2 2 3
A(:,:,3) =
2 5 2 3
5 2 3 4
3 4 1 5
3 4 1 1
If you read the help carefully, you will notice that the randi function accepts any number of dimensions. You may do randi(10,3,3,3)
randi(10,3,3,3)
ans(:,:,1) =
9 10 3
10 7 6
2 1 10
ans(:,:,2) =
10 10 2
2 5 5
10 9 10
ans(:,:,3) =
8 1 7
10 9 8
7 10 8

How to convert an Image matrix into a vector in Matlab

I cant seem to figure this out:
I need to reshape a matrix into a vector and so far I have this:
img = imread('image.png');
grayImage = rgb2gray(img);
imageArray = reshape(grayImage, r, c);
Which outputs something like:
imgVector=[1 2 3 4 5 6 7 8 9 0]
My problem is I need it to do something like this:
imgArray=[1 2 3
4 5 6
7 8 9]
Reshaped into:
imgVector=[1 2 3 6 5 4 7 8 9]
I hope that make sense. Basically I need it to be unzipped so it goes from left to right and then right to left after the next row. Any help would be appreciated. Thank you in advance.
Fundamentally what you're trying to do is flip each row left-to-right, so the built-in function fliplr will do the trick.
To do it in a single step, just select every other row in your indexing operation:
>> imgArray=[1 2 3; 4 5 6; 7 8 9]
imgArray =
1 2 3
4 5 6
7 8 9
>> imgArray(2:2:end,:)=fliplr(imgArray(2:2:end,:))
imgArray =
1 2 3
6 5 4
7 8 9
Then you can turn it into a vector by reshaping.
imgVector=reshape(imgArray',1,[]);
#%transpose the array----^
Since reshaping is done column-wise, transpose the array first to get it in the format you want.
You can use the fliplr function, which inverts the order of a vector.
Here is a simple example:
A = [1 2 3;4 5 6;7 8 9];
A =
1 2 3
4 5 6
7 8 9
A(2,:) = fliplr(A(2,:));
A =
1 2 3
6 5 4
7 8 9
So could use a loop an flip every other row for your entire image. Hope that helps!

average of an array

I have a matrix with 4 rows and 400,000 columns. I need to obtain the average of 4 successive rows. That is, average of row 1 to row 4, row 5 to 8, etc.
The 4 columns should be maintained as such. I know that this may be a kindergarten level problem, but I appreciate any hints to write a program in Matlab; I have a little experience writing Matlab programs.
An example of the data can be as follows:
[1 2 3 2;
5 6 7 2;
9 6 7 6;
5 2 3 2;
9 8 7 6;
6 5 4 3;
4 3 2 1;
9 8 7 6]
I want the result as:
[5 4 5 3],[7 6 5 4]
It is not entirely clear to me how your data is layed out, so I'll give you a solution to what I think you mean.
Suppose you have
a = [
1 2 3 4 %# row 1
2 3 4 5
3 4 5 6
4 5 6 7 %# row 4
5 6 7 8 %# row 5
...
]; %# row m
and you want the average down the columns of rows 1 through 4, 5 through 8, etc.
You can do that simply by
averages = reshape(mean(reshape(a,4,4,[])),[],4)
breakdown:
A = reshape(a,4,4,[])
rearranges the data in your matrix into a 3D array. Each 3D "layer" of this array is a 4x4 matrix.
B = mean(A)
This takes the average along the columns (direction 1). Read up on help mean for more information.
C = reshape(B,[],4)
This rearranges the array of averages back to a Nx4 matrix, where N=m/4.