I want to loop an equation over multiple rows
Show older comments
I know how to do this in excel but not in Matlab.
I have a huge matrix and in that I want to create a equation for only three column( a,b and c) s, but on every row. I want it to add a column with the answers to the equation(x).
The equation is x=a*b^c
In each row of columns a b and c the integers are different, so I can't use a simple number instead.
I am thinking the answer to this is a for loop but I am a begginer at Matlab so may be wrong.
In excel the way i would do this is create the equation in the top row and drag down,if that makes sense.
Accepted Answer
More Answers (1)
Image Analyst
on 15 Aug 2021
Try this;
columnsToProcess = [4, 9, 17]; % Whatever....
[rows, columns] = size(x); % Size of original matrix.
for col = columnsToProcess % Process only these particlar columns.
for row = 1 : rows
% Get the a, b, and c parameters.
% These vary on a row-by-row basis.
a = however you get it.
b = however you get it.
c = however you get it.
x(row, col) = a * b ^ c;
end
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!