Robbie.b
Robbie.b

Reputation: 21

Matlab: Loop through rows of matrix to calculate lowest value

I am working on a program that will take my 24x24 matrix (P6) and multiply each row by a 24x1 matrix R. Basically I would like to do row(1)*R, check value, update, row(2)*R, etc

This is my current code:

P6 = rand(24);
R = rand(24,1);
best=100;
for i=1:24
    X(i)=P6(i,:)*R
    if X(i) <= best
        best = X(i)
    end
end

Presents errors such as:

Attempting to access X(3); index out of bounds because number(X)=1

and

matrix dimensions must agree

Any help with this would be greatly appreciated! Thank you in advance!

Upvotes: 1

Views: 63

Answers (1)

OmG
OmG

Reputation: 18838

Your approach is not correct. If you multiply p6 to R you can get minimum by min function:

best = 100;
result = P6*R;
[best_val, best_ind] = min(result);
% check best_val with best value at the end

Upvotes: 1

Related Questions