Extract max/min values from a table in workspace in Matlab - matlab

Back to basics. How can I extract max and min values from 'variable' table in workspace? min(nameofvariable) don't work, returns an error message 'Subscript indices must either be real positive integers or logicals'.
Thanks for your help!

Assuming you have a matrix called nameofvariable, the correct syntax is indeed:
min(nameofvariable)
This will give you the column wise min
For the full minimum:
min(nameofvariable(:))
The error you have suggests that you have a variable named min (try to avoid this at all times), try:
clear min
min(nameofvariable)

Related

poLCA ALERT values that are not positive integers

everybody.
I am trying to run an LCA using poLCA package. I have six categorical variables, from which one is binary (gender) and the other ones range from 3 to 7 categories. I keep getting an alert message as follows:
ALERT: some manifest variables contain values that are not
positive integers. For poLCA to run, please recode categorical
outcome variables to increment from 1 to the maximum number of
outcome categories for each variable.
I applied as.integer() and I tried recoding replacing level 0, but it is still not working. Any ideas on how this could be, please? Thank you in advance!
Below, my code:
f<- with(mydata, cbind(v1, v2, v3, v4, v5, v6)~outcome)
LCA<-poLCA(f, data, nclass=6, nrep=50)
summary(LCA)
Output:
ALERT: some manifest variables contain values that are not
positive integers. For poLCA to run, please recode categorical
outcome variables to increment from 1 to the maximum number of
outcome categories for each variable.
Manifest values needs to be an integer starting from 1 and not 0, hence it shows that error.
You need to add 1 to your dataset.
data=data+1;
Now run the function :)
For continuous variables (if you have in your dataset, you can round them to nearest digit)
round(mydata, digits=0)

how can i find the value of 'X' which 'Y' is zero in it?

I have a 3000*3000 matrix. which this matrix is the value of Force. The name of Matrix is 'forceZ1'. First I choose one of the columns ( the 1234th column) now I want to know which row has the value of zero. I tried the method 'find' but the result was nothing( it said that forceZ1 never get the value zero) whereas when I plot the forceZ1 on its 1234th column I see that in two point it is zero. I want to know the exact value of that points! Help me :)
Maybe it's a small value close to zero but not exactly zero. You can try:
find(abs(forceZ1(:,1234)) < 1e-5)
You can try with different thresholds e.g. 0.1 0.001 until you don't get too many indices, i.e. apply to your situation.

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);

How do i set a row within a matrix to 0?

I have a matlab problem where I need to find the maximum number in a matrix, and then find the next greatest value in the matrix that is not in the same row or column as the previous one.
My thought process is that I will find the maximum value in the matrix and then figure out which row and column it is in and then set the rest of the values in the row and column to 0. so far I have this.
a=rand(5)
[row,column]=find(a==max(max(a)))
I can find which row and column the maximum is but that is about it. Can somebody help me with the next step or a better way to go about writing this program? Thank you!
What you need is:
a(row,:)=0;
So, in total:
a=rand(5)
[row,column]=find(a==max(max(a)))
a(row,:)=0;
[row2,column2]=find(a==max(max(a)))
if you have negatives values in a, you can also do:
a(row,:)=-inf;

MATLAB: What's [Y,I]=max(AS,[],2);?

I just started matlab and need to finish this program really fast, so I don't have time to go through all the tutorials.
can someone familiar with it please explain what the following statement is doing.
[Y,I]=max(AS,[],2);
The [] between AS and 2 is what's mostly confusing me. And is the max value getting assigned to both Y and I ?
According to the reference manual,
C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.
[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned.
I think [] is there just to distinguish itself from max(A,B).
C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension (the rows) of A.
Also, the [C, I] = max(...) form gives you the maximum values in C, and their indices (i.e. locations) in I.
Why don't you try an example, like this? Type it into MATLAB and see what you get. It should make things much easier to see.
m = [[1;6;2] [5;8;0] [9;3;5]]
max(m,[],2)
AS is matrix.
This will return the largest elements of AS in its 2nd dimension (i.e. its columns)
This function is taking AS and producing the maximum value along the second dimension of AS. It returns the max value 'Y' and the index of it 'I'.
note the apparent wrinkle in the matlab convention; there are a number of builtin functions which have signature like:
xs = sum(x,dim)
which works 'along' the dimension dim. max and min are the oddbal exceptions:
xm = max(x,dim); %this is probably a silent semantical error!
xm = max(x,[],dim); %this is probably what you want
I sometimes wish matlab had a binary max and a collapsing max, instead of shoving them into the same function...