rounding specific columns in matlab - matlab

I have 3 columns of data, but I need to round only the first and the last column and the second let as it is. So my "dream" format - round column, not round column, round column.
Could someone help me please?
.

A(:,1)=round(A(:,1));
% repeat above line for each column you want.
If you have many columns, you may want to store them in an index and loop
to_round=[1,3];
for ii in length(to_round)
A(:,to_round(ii))=round(A(:,to_round(ii)));
end

Related

How to replace rows in a table with 'NaNs', leaving the matrix dimension unchanged? (Matlab)

Let's assume the following:
A=rand(10,5);
How can I replace the first 3 rows of this table with NaN's, without the matrix losing its original dimension 10x5? Thanks in advance
Just index into the first three rows but ensure you select all of the columns and replace the entries with nan:
A(1:3,:) = nan;
I have a minor comment. The variable A is not a table, but it is a matrix. Please ensure you use the right terminology from now on, as someone may confuse what you're saying with the actual table construct.

How can i change indices of a matrix in Matlab?

My problem is i want to assign some numbers to indices of a matrix. For example if I remove first row and first column of a matrix, then in remaining matrix 3th row and 4 column would actually be 4th row and 5th column in the first place.
I can do it with Array1(Array2) , however my code will have many seperate recursions so it is frustrating to keep track of everything. So, is there an once and for all way to map original 1..n indices to remaining matrix even after I remove rows and columnsth
Thanks in advance
You can do something like this as per beaker's suggestion
originalMatrix = magic(4)
dimension = size(originalMatrix)
indexMatrix = zeros(dimension(1), dimension(2))
for i = 1:numel(indexMatrix)
indexMatrix(i) = i
end
and remove the required row and column from indexMatrix.

MATLAB Extracting Column Number

My goal is to create a random, 20 by 5 array of integers, sort them by increasing order from top to bottom and from left to right, and then calculate the mean in each of the resulting 20 rows. This gives me a 1 by 20 array of the means. I then have to find the column whose mean is closest to 0. Here is my code so far:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
MeanArray= mean(transpose(NewArray(:,:)))
X=min(abs(x-0))
How can I store the column number whose mean is closest to 0 into a variable? I'm only about a month into coding so this probably seems like a very simple problem. Thanks
You're almost there. All you need is a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
ColNum = find(abs(mean(NewArray,1))==min(abs(mean(NewArray,1)))); %// gives you the column number of the minimum
MeanColumn = RandomArray(:,ColNum);
find will give you the index of the entry where abs(mean(NewArray)), i.e. the absolute values of the mean per column equals the minimum of that same array, thus the index where the mean of the column is closest to 0.
Note that you don't need your MeanArray, as it transposes (which can be done by NewArray.', and then gives the mean per column, i.e. your old rows. I chucked everything in the find statement.
As suggested in the comment by Matthias W. it's faster to use the second output of min directly instead of a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
[~,ColNum] = min(abs(mean(NewArray,1)));
MeanColumn = RandomArray(:,ColNum);

MATLAB Loop Programming

I've been stuck on a MATLAB coding problem where I needed to create market weights for many stocks from a large data file with multiple days and portfolios.
I received help from an expert the other day using 'nested loops' it worked, but I don't understand what he has done in the final line. I was wondering if anyone could shed some light and provide an explanation of the last coding line.
xp = x (where x = market value)
dates=unique(x(:,1)); (finds the unique dates in the data set Dates are column 1)
for i=1:size(dates,1) (creates an empty matrix to fill the data in)
for j=5:size(xp,2)
xp(xp(:,1)==dates(i),j)=xp(xp(:,1)==dates(i),j)./sum(xp(xp(:,1)==dates(i),j)); (help???)
end
end
Any comment are much appreciated!
To understand the code, you have to understand the colon operator, logical indexing and the difference between / and ./. If any of these is unclear, please look it up in the documentation.
The following code does the same, but is easier to read because I separated each step into a single line:
dates=unique(x(:,1));
%iterates over all dates. size(dates,1) returns the number of dates
for i=1:size(dates,1)
%iterates over the fifth to last column, which contains the data that will be normalised.
for j=5:size(xp,2)
%mdate is a logical vector, which is used to select the rows with the currently processed date.
mdate=(xp(:,1)==dates(i))
%s is the sums up all values in column j same date
s=sum(xp(mdate,j))
%divide all values in column j with the same date by s, which normalises to 1
xp(mdate,j)=xp(mdate,j)./s;
end
end
With this code, I suggest to use the debugger and step through the code.

how to use tables in Matlab

I want to know please how can I fill in a table, row by row, by numbers, and then to color in each row the cell that has the higher number in it.
I searched the web a little and found this "set(handles.uitable2, 'Data', {5,6,4})" but this is not helping me because i need to fill in row by row, and in this method the row data is been replace.
this is the table. as you see there is 7 rows and 10 columns. in each columns there is the correlation score of the plate digit against the samples digits (0-9).
this is how I call the correlation function[scores] = compute_corr(digit); I'm executes this call 7 times for each plate digit. scores is an array that saves in each call the correlation scores and digit is one digit from the plate.
thanks in advance.
I don't believe there's a way to update the data incrementally. So you should maintain an array containing your data, update that row by row, and call set(...,'Data',actualData) when it changes.