Info

This question is closed. Reopen it to edit or answer.

Creating a matrix from an original matrix of 2464*2464

1 view (last 30 days)
I am trying to use following code:
CV = [56:56:2464]
for i = 1:44
if i==1
j=1
elseif i>1
j=CV(i-1)+1
end
TCI(i)=sum(sum(M(:,j:CV(i))))
for ii = 1:LengthIO
for jj = j: CV(i)
if jj< CV(i)
ImportsCoefficientMatrix(ii,jj) = ImportsBreakdown(ii,jj)/TCI(i);
end
end
end
end
But it keeps on running forever.
All I want to do is create a new matrix which has same dimensions as M(2464*2464) but every element of it in first 56 columns then it is divided by sum of first 56 columns. If is in next 56 columns it is divided by next 56 columns and so on..
Can someone help??
Thanks
  2 Comments
Roger Stafford
Roger Stafford on 16 Nov 2017
To fully understand your code we need to know what the CV values and the LengthIO scalar are equal to. Your figures of 44 and 56 (44*56=2464) don't match your figure of 2000 for the total number of columns. Also I see no reason why the code should run "forever"; it looks as if it should involve something in the neighborhood of four million steps which ought not to take very long. If each of your groups of columns has the same number of columns in it, namely 56, the use of CV here seems very awkward. Please give us some more accurate information.
Siddhartha Sharma
Siddhartha Sharma on 17 Nov 2017
Hi Roger, Yes it is 2464 by 2464. But when I normally load a matrix that long it takes about 30 seconds and when I run this code it continues for over half an hour when I break it. I have addede details of CV in above code for reference.

Answers (1)

the cyclist
the cyclist on 16 Nov 2017
Edited: the cyclist on 17 Nov 2017
Does this do what you want?
% Size of one dimension of your matrix
N = 44*56;
% Define some pretend data (You don't need this step)
rng default
M = rand(N,N);
% Transform array into slices of 56 columns
Mr = reshape(M,N,56,[]);
% Perform the division
Mr_sum = sum(sum(Mr,1),2);
Mr_normalized = Mr ./ Mr_sum;
% Transform back
M_normalized = reshape(Mr_normalized,N,N);
It wasn't clear to me if you wanted to divide by the grand sum of all the elements in the first 56 columns, or divide each column individually.
  5 Comments
the cyclist
the cyclist on 17 Nov 2017
OK. My code should do exactly what you want, without using for loops, so it should be fast.
I added some comments.
Siddhartha Sharma
Siddhartha Sharma on 17 Nov 2017
Thanks a lot!! That is very helpful. I think it will take me some time to understand how powerful matlab's reshape and index functions are.

Community Treasure Hunt

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

Start Hunting!