Reshaping a 3D data in matlab - matlab

I have this 3-D data in matlab of dimension 3x3x10. What I want is to reshape it to a data of size/dimension 10x9. Such that for each i,j,1:10. I have one column in this new data. How can I do it. I tried using reshape(data, 10,9). and it gave me a data structure of size 10x9. However, I doubt how it arranged it.
I want to reshape it just that if new_data is my new data of size 10x9, my first column is old_data(1,1,:). The second column of my new data is old_data(1,2,:) and so on

I personally abhor the use of for loops in Matlab. I think a better way would be to permute the dimensions, then reshape. Also, it is not clear the order in which you want to arrange the data. You should be able to achieve what you want by fiddling with the dimension numbers in the permute function call.
% B is a 3x3x10, permute to 10x3x3, then reshape into 10x9
% Change the ordering of the dimension in permute to achieve the desired result
% remember: reshape takes elements out of B column by column.
newB = reshape(permute(B,[3 1 2]),10,9);
% newB is a 10x9 matrix, where each row is the entries of the 3x3 matrix
% the first column is element 1:9:82

You could do this:
for i = 1:size(x,3)
x(:,:,i) = x(:,:,i).';
end
newData = reshape(x,[],size(x,3)).'

Related

Converting matrix into 128x128 picture

I am pretty new to Matlab, and I am trying to convert my data file into an
128x128 matrix in order to display an image. So, in my file I have 3 columns with 16384 numeric values in each of them, and I need to have 128x128x3 matrix, like a format of image. I was trying method reshape, but it did not work for me, I am getting an error such
Error using reshape
To RESHAPE the number of elements must not change.
Here is my code
x = load('out.txt');
B = reshape (x,128,128);
What would be the best solution for this problem?
If you want to make a 128x128x3 matrix, you have to say so to reshape:
B = reshape (x,128,128,3);
You can leave one of the values out, but you have to replace it with an empty array:
B = reshape (x,128,[],3);
This will calculate the size for that dimension.

Selecting the vector with the largest sum Matlab

I'm looking for a way to select the vector that has the largest sum. Is there a simple way of doing this? I was thinking of writing a loop, but I'm not sure how to loop over a set of vectors.
Thanks for your help!
For the case in which the vectors have the same length (as stated in the comment), I think a simple loop-free way would be to build a matrix from each vector and fetch directly the row (or column) with the largest sum:
clear
clc
RandMat = rand(8,10);
[~,Ind] = max(sum(RandMat,2)); %// Get row index for largest sum. If you want the column, use 1 instead.
MaxRow = RandMat(Ind,:); %// Index in original matrix to get the vector. If you want the column, use RandMat(:,Ind);
If vectors don't have the same length then you would need to pad the missing values with NaN for example to use a regular matrix, otherwise you would need a cell array.
If you prefer a solution in which you don't have to build a matrix then you could loop through each individual vector and store the sum in a variable, then compare the sums at the end. If you would like such a solution please ask!

Putting vectors of different size in a matrix

I am trying to add a number of vectors in a Matrix where each row represents a vector, but it gives me "Subscripted assignment dimension mismatch." error. The main problem is that each vector has a different size. I tried to add zeros at the end of the short vectors but I couldn't do it. Any Help.
Example:
%signal is a vector of data.
[x(1,:),y(1,:)] = findpeaks(signal1);
[x(2,:),y(2,:)] = findpeaks(signal2); %error as the peaks count in signal 2 is not the same as in signal 1.
OK, given two vectors of unequal length,
A=rand(1,10)
B=rand(1,5)
the proper way to deal with this is to use a cell array
D={A;B}
And then you can get whatever elements you want like this, for example:
D{1}(1:3) %// A(1:3)
If you don't want to use cells, you can add rows using this little function that adds row vector M to matrix F
addRow=#(F,M) [F NaN(size(F,1),size(M,2)-size(F,2));M NaN(1,size(F,2)-size(M,2))]
you would use it like this:
F=A
F=addRow(F,B)

Cropping matrix

for examples, I have a 6x6 matrix, then I want to take out the small matrix which is located in the center of that matrix, say 2x2. Is there any smart way to do it ? Or I have to loop through the old matrix and then copying values to new one?
Thank you very much.
Of course you can. try for instance
A = rand(6,6); % // big matrix, an example
B = A(3:4,3:4); % // central sub matrix obtained using indices
which (in this case) is also equivalent to
B = A([3 4],[3 4]);
In general you can extract subvectors from a vector selecting the indices you are interested to.

How to have normalize data around the average for the column in MATLAB?

I am trying to take a matrix and normalize the values in each cell around the average for that column. By normalize I mean subtract the value in each cell from the mean value in that column i.e. subtract the mean for Column1 from the values in Column1...subtract mean for ColumnN from the values in ColumnN. I am looking for script in Matlab. Thanks!
You could use the function mean to get the mean of each column, then the function bsxfun to subtract that from each column:
M = bsxfun(#minus, M, mean(M, 1));
Additionally, starting in version R2016b, you can take advantage of the fact that MATLAB will perform implicit expansion of operands to the correct size for the arithmetic operation. This means you can simply do this:
M = M-mean(M, 1);
Try the mean function for starters. Passing a matrix to it will result in all the columns being averaged and returns a row vector.
Next, you need to subtract off the mean. To do that, the matrices must be the same size, so use repmat on your mean row vector.
a=rand(10);
abar=mean(a);
abar=repmat(abar,size(a,1),1);
anorm=a-abar;
or the one-liner:
anorm=a-repmat(mean(a),size(a,1),1);
% Assuming your matrix is in A
m = mean(A);
A_norm = A - repmat(m,size(A,1),1)
As has been pointed out, you'll want the mean function, which when called without any additional arguments gives the mean of each column in the input. A slight complication then comes up because you can't simply subtract the mean -- its dimensions are different from the original matrix.
So try this:
a = magic(4)
b = a - repmat(mean(a),[size(a,1) 1]) % subtract columnwise mean from elements in a
repmat replicates the mean to match the data dimensions.