Clear Filters
Clear Filters

How to make for loop to make various mathematical operations. Using matrices.

1 view (last 30 days)
clear all
t = zeros(size(magic,1),1);
pi = zeros(size(magic,1),size(magic,2));
for i=1:size(magic,1)

Above is my code I am currently using for testing. My idea is for every values in the row in magic, divide it by sum of that row (AKA it should be divided by 1 number).

Answers (3)

Purushottama Rao
Purushottama Rao on 16 Dec 2016
Edited: Purushottama Rao on 16 Dec 2016
If it is a magic matrix, then its fairly simple..
ans= m/s(1)

José-Luis on 16 Dec 2016
your_array = magic(5); %don't use a built-in function as a variable name
your_result = bsxfun(@rdivide,your_array, sum(your_array,2));

Steven Lord
Steven Lord on 16 Dec 2016
If you're using release R2016b or later, you can use implicit expansion.
M = magic(5);
Y = M./sum(M, 2);

Community Treasure Hunt

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

Start Hunting!