I want to insert vector A iteratively in mxn Matrix and in the last column I want to insert a double value. Each time A and constant are changing. E.g. in first iterating A=[1 2 2 4 5] and constant = 3.78. In second iteration A= [3 2 9 8 1]and constant 1.09 and so on... I want this output. [1 2 2 4 5 3.78] [3 2 9 8 1 1.09] [2 3 1 8 1 2.01] and in the end maximum value of last column (double value) is computed. the corresponding array should be displayed. e.g. in the above case max = 3.78 so [1 2 2 4 5] should be displayed How can I do this?

 Réponse acceptée

KSSV
KSSV le 16 Mar 2016
clc; clear all ;
N = 5 ;
iwant = [];
for i = 1:10
A = rand(1,N) ; % your A matrix which changes
k = rand(1,1) ; % your constant which changes
iwant = [[A,k] ; iwant] ; % matrix which you want
end
% Print array knowing maximum value
Max = iwant(3,end) ; % Pick a arbitrary maximum value
% Get the row position for maximum value
[r] = find(iwant(:,end)==Max) ;
% Print tthe array
iwant(r,1:end-1)

4 commentaires

Guillaume
Guillaume le 16 Mar 2016
Modifié(e) : Guillaume le 16 Mar 2016
There is no need to use find to find the location of the maximum. It's returned by max as a second output:
[maxval, row] = iwant(:, end);
as a side note, I wouldn't use variable names that differ from function names only by the case. Too much potential for a bug.
Nida
Nida le 17 Mar 2016
Thanks!Worked for me :)
I have a matrix i.e rMatrix, I want to get its two rows based on the Maximum value of the last column and then store the two rows in another matrix B. Here my code is fetching only one row with maximum value. I want to modify my code to get two rows. (Here maxMQ is the maximum value of last column)
display(rMATRIX);
maxMQ = max (rMATRIX(:,end));
display(maxMQ);
[r] = find(rMATRIX(:,end)==maxMQ) ;
fprintf('This is the best row');
display(rMATRIX(r,1:end-1));
%***********FOr Example**********
1 2 9 4 7 3.09
3 4 2 1 8 2.76
4 2 1 0 2 4.23
2 0 3 1 5 1.67
I want to get B matrix (Without displaying the last column)
B = 1 2 9 4 7
4 2 1 0 2
As stated by me and Steven, there is no need for find. You can replace these two lines:
maxMQ = max(rMATRIX(:, end));
[r] = find(rMATRIX(:, end) == maxMQ);
By just this one line:
[maxMQ, r] = max(rMATRIX(:, end));
It's a lot faster, and also guaranteed to only return one row, which seems to be your assumption.
As for your question, please start a new question. I'd rather it not be answered in a comment to Siva's answer as it's not a very good answer.

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 16 Mar 2016
Modifié(e) : Stephen23 le 17 Mar 2016

0 votes

Here is the simple solution without find:
% preallocate matrix and vector:
mat = NaN(3,5);
vec = NaN(3,1);
% add your values in a loop:
mat(1,:) = [1,2,2,4,5];
vec(1,1) = 3.78;
mat(2,:) = [3,2,9,8,1];
vec(2,1) = 1.09;
mat(3,:) = [2,3,1,8,1];
vec(3,1) = 2.01;
% find maximum and display:
[val,idx] = max(vec);
disp(mat(idx,:))
The code displays the corresponding row of mat in the command window:
1 2 2 4 5

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by