Selecting cells in a table using for-loop - matlab

I need to obtain individual values from cells incrementally.
For example in a table called "T":
Can Matlab create a loop to yield the following?:
T(1,1)
T(1,2)
T(1,3)
T(1,4)
and so forth.

First of all the first parameter in MATLAB equals the row, the second the column. So following your picture you would want to extract a complete column. Then you have to loop over your first parameter and keep the 2nd constant.
So not sure if this is really what you want but:
for k = 1:size(T,1)
T(k,1)
end
would write all elements in column 1 row after row. This is because size(T,1) returns the No. of rows in your Table. So by looping over all rows you return one column. Easier with the same result would be:
T(:,1)
which would return the whole first column.
If you don't want them to be displayed but to use them as entries for another variable use:
new_T = T(:,1);
The ; makes sure that the command isn't displayed on the console. And the new_T would have all entries from column 1.
Another example:
new_T2 = T(1:500, 4);
This would result in the first 500 elements from column 4.
Hopefully this solves your question, if not please explain your question more detailed.

Related

How to get a collective output of multiple loop run using a selection condition in Matlab?

I have a table (L-arrival) of 279 rows and 252 columns. Only the first column has values while others are just NaN. The cells in the first column have multiple values (i.e. some have 1, some have 4 number of values). First of all, I am trying to select a single maximum value from each cell of the first column so that I can have a column of a single value for each cell only. Then I want to do this in a loop so that for every new value that I get, they are sorted and only the maximum values are chosen. Finally, I want to make a collection of these values obtained from multiple runs for each cell. Can anyone suggest to me how it can be approached in MatLab?I tried using the following code but didn't work well.
for b=1:279
m = numel(cell2mat(L_arrival(b,1)));
g(b)=mat2cell([cell2mat(g(b)); cell(L_arrival(b,1))]',[1 2]);
end

Finding the max element in a column of a matrix excluding certain rows

Consider I have an nxn matrix. My goal is to find the max element of the first column, swap the row containing that largest element and the first row. Next, I want to find the max element of the second column, excluding the first row, then swap the row of this new max element and the second row. Again, finding the max element of the jth column excluding rows 1:j-1, then swapping the max element row with the jth row, up until the n-1th column (as during the nth column, I would only be able to choose from the nth row).
My current setup for this is as follows
for j = 1:n-1
[~,row]=max(A(j:n,j));
temp = A(row,:);
A(row,:)=A(j,:);
A(j,:)=temp;
...
While the switching function works well enough, [~, row]=max(A(j:n,j)) is able to, and consistently does for the matrix I'm specifically working on, output row 1 during the second iteration of j. My thought process behind this was that j:n represents the rows we want to check. Noting j=2, for its second iteration, I hoped this would search row 2-to-n; however, it seems to still check every row.
While this question has been asked before, the answer, I found, was this same line of code.
You are using [~,row]=max(A(j:n,j));. Let's say you are in iteration j=2. max will only consider the input from the 2nd row on. A return value of row=1 indicates a maximum in the second row of A. The first row you put into the function. The max function has no clue that you actually input something larger. You have to correct for this.
n=5
A=magic(n); %just some input data
for j = 1:n-1
[~,row]=max(A(j:n,j));
row=row+j-1;
temp = A(row,:);
A(row,:)=A(j,:);
A(j,:)=temp;
end
by the way, matlab can do row swapping without helper variable:
for j = 1:n-1
[~,row]=max(A(j:n,j));
row=row+j-1;
A([row,j],:)=A([j,row],:);
end

Creating new rows in a matrix, where the elements of these rows have to be different from previous rows(same row cannot occur twice) Matlab

I'm trying to generate a 3x2 matrix. Each row is generated using randperm(3,2). This means each row is generated as a vector with 2 unique integers with values between 1 and 3.
The problem is that I want each new row to be different than all the previous. For example, if one row is [1 3] then no other row can be:
[1 3], nor
[3 1].
I tried checking the sum AND the multiplied value of each newly created row. (Using our example 1+3=4 and 1*3=3)
My idea is that the multiplied value and the sum value of each new generated row is compared to the multiplied value and sum value of every other row that comes before it. If any of these values are the same (which means we will get a repetition), we keep generating a new row using randperm(3,2) until a completely new row is obtained.
My code checks each each row before one at a time, and "forgets" every other row that it previously checked. It does not take into consideration ALL the previous rows, instead it only iterates back one step at a time. I tried using something like parents(i:-1:1) instead of parents(i-k,1) etc but couldn't make it work.
Question: How can I do this comparison?
parents=randperm(3,2);
for i=2:3
parents=[parents; randperm(3,2)]
for k=1:i-1
while prod(parents(i,:))==prod(parents(i-k,:)) && sum(parents(i,:))==sum(parents(i-k,:))
parents(i,:)=randperm(3,2)
end
end
i=i+1;
end
Download this function, then use it as follows:
% generate all the possible permutations
p = permn(1:3,2);
% generate a random permutation of the indices and take the first three
r = randperm(size(p,1));
idx = r(1:3);
% take three random rows from the possible permutations using the indices
result = p(idx,:);
This way:
you will never obtain a row identical to another one in your result matrix
you won't be forced to use a "shuffle until condition is met" approach
you have a reusable and flexible approach

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.

Quick way to reference an index of values inside one array column like bitzer(:,5)?

I want to reference an index of values inside the 5th column of an array like blitzer.
E.g. I want to access, say, all values of blitzer(:,5) where blitzer(:,4) < 10. This outputs an index of values. So maybe I could set blitzer5 = blitzer(:,5), and then call blitzer5(blitzer(:,4) < 10).
But is there a quick way to do this without having to create an entirely new vector? Ideally I'd like to call blitzer(:,5)[blitzer(:,4) < 10]. If so, how?
You can give vertical index from the matrix itself:
blitzer(blitzer(:,4) < 10,5)
This will give you elements from the 5-th column, where the corresponding elements in the 4-th row are less than 10.
If you want to reference the 8th element in the 5th column of an array, you can write
theElement = blitzer(8,5)