Write a C program to determine the maximum value among the elements of the array, the first and last position of the element with this value.
I tried to determine the position of the first maximal element, but failed.
Related
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
I have a 133120x4 matrix in Matlab.
I would like to find the largest value in each row, and divide every element in that row by that particular value.
Do I need to use some sort of loop? For example: I find the amount of rows in that matrix (133120), and iterate the loop that amount of times, then I go row by row and use the max function to return the largest value in that row, and divide each element in that row by the returned value from max.
Or is there a quicker way of doing this?
Thanks
EDIT (for clarification):
lets call my 133120x4 matrix A. I want to divide every element in a row by the largest value in that row. Since max and element-division are vectorized, would the solution simply be:
A_normal = A / max(A)
resulting in a 133120x4 matrix, but within each row, the largest value would be 1.
Is this correct? EDIT: It is not correct, and am still trying to figure out solution. Help from the community is greatly appreciated
Compute the maximum with max, repeat the result N(=4) times so there is one per each element and then element wise division
!
newMat=mat./repmat(max(mat,[],2),[1 size(mat,2)]);]
or in R2016b or newer just
newMat=mat./max(mat,[],2);
I have a 3x3 array, called q, for example:
q=[1,2,3;4,5,6;7,8,9]
u=one of the element in array q
[row,col]=find(q==u) % to find the row and column number of element u
how to access the elements with row number greater than the row number of element u?
i really appreciate the help, if someone can share the ideas.
I get the error
??? Index of element to remove exceeds matrix dimensions.
Error in ==> myfile at 111
C(i)=s{i,3};
the code being:
C=zeros(num_of_tris,1);
for i=1:size(C,1)
C(i)=s{i,3};
end
I'm not showing the code for creating s, but I assume it's beside the point as s only appears on the right hand side of the assignment...
why does it say element to remove? which element am I removing?
Ok, so here's what is happening. s is probably initialized to an empty cell (NOTE: need not be entirely empty -- see last paragraph). So, indexing an element of s as s{i,3} returns []. The MATLAB operation to remove an element of a vector is
C(i)=[];
So when you loop through, you're removing the elements of C one by one, and eventually, the index i exceeds the size of the (now diminished) vector.
Here's a small example that reproduces your problem:
s=cell(10,5); %#initialize s to an empty cell
%#note that any cell returns []
s{3,4}
ans =
[]
%#This is your code from above
C=zeros(10,1); %#initialize C
for i=1:size(C,1)
C(i)=s{i,3};
end
??? Index of element to remove exceeds matrix dimensions.
You'll find that the index i when you get this error is numel(C)/2+1. In other words, till i=5 (in this example), you're removing every odd element of C and at i=6, the number of elements remaining in C is 5, and so you get an index out of bounds error.
NOTE:
s need not even be entirely empty. Consider this example:
s=cell(10,1);
s([1,2,6,8])=num2cell(rand(4,1));
C=zeros(10,1);
for i=1:numel(C)
C(i)=s{i};
end
??? Index of element to remove exceeds matrix dimensions.
Say you have an array, data, of unknown length. Is there a shorter method to get elements form a starting index to the end than
subdata = data(2:length(data))
You can use end notation to indicate the last element. data(2:end) returns a vector containing elements in the vector data from element 2 to the last element. Or if data is a character array, it returns the second character all the way to the last character. And data(end) returns the last element.
This can be done with matrices too, i.e. data(2:end,5:end). Additionally you can use it as an operand, i.e. data(2:end-1) , data(2:end/2).
In this context, end serves a different purpose from its use at the end of functions/loops/switches.