필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Creating a matrix from an original matrix of 2464*2464

조회 수: 1 (최근 30일)
Siddhartha Sharma
Siddhartha Sharma 2017년 11월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Roger Stafford
Roger Stafford 2017년 11월 16일
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 2017년 11월 17일
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.

답변 (1개)

the cyclist
the cyclist 2017년 11월 16일
편집: the cyclist 2017년 11월 17일
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
the cyclist
the cyclist 2017년 11월 17일
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 2017년 11월 17일
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!

Translated by