Error when manipulating values on a matrix

Hello there,
I've got a few problems with my code. I'm going to explain: I have a matrix that is randomly generated: Populacao1 = [((randi([-1 1], n_individuos, n_centrais*int_tempo)))];
n_individuos=10 (or another number) n_centrais=2 (or more) and int_tempo = 10 (or more)
Populacao1 has only 1, -1 and 0 values. Now the problem begins.
I want to be able to keep 4 matrix (caulda_turbinado_c1, caudal_bombado_c1 and caulda_turbinado_c2, caudal_bombado_c2)
caudal_turbinado_c1 is the first (n_centrais*int_tempo)/2 where the the value equals 1 and caudal_bombado_c2 is when it equals -1. caudal_turbinado_c2 and bombado_c2 are the same in the other half of the matrix. the problem is that it doesn't work. Don't know why :s
for i=1:n_individuos
for j=1:int_tempo*n_centrais
if j <= int_tempo
if(Populacao1(i,j)==1)
caudal_turbinado_c1(i,j) = 100;
if(Populacao1(i,j)==-1)
caudal_bombado_c1(i,j)=200;
end
end
end
if j > int_tempo && j<=int_tempo*n_centrais
if(Populacao1(i,j)==1)
caudal_turbinado_c2(i,j-int_tempo) = 400;
if(Populacao1(i,j)==-1)
caudal_bombado_c2(i,j-int_tempo)=200;
end
end
end
end
end
Thanks in advance

댓글 수: 4

If you provide data to test your code, it will be helpful
Jan
Jan 2013년 1월 2일
Pleas explain "it doesn't work" with any details. Do you get an error message or do the results differ from your expectations?
The result is different from my expectations. I was expecting that my could would divide the matrix into to halfs, and in the first one it would put 100 if =1 and 200 if the value =-1 and in the second half it would put 400 if value =1 and 200 if value =-1. If the value was 0 it would do nothing. The thing is that it doesnt work. Don't know why. I resolve this spliting the matrix, do the process and then merge the matrix back together. But it would be much better if this work.
Azzi i think i've put all the values in the explanation. What is missing? thanks for the help!

댓글을 달려면 로그인하십시오.

 채택된 답변

per isakson
per isakson 2013년 1월 4일
편집: per isakson 2013년 1월 4일

0 개 추천

The statement
caudal_bombado_c1(ii,jj)=200;
in
if (Populacao1(ii,jj)==1)
caudal_turbinado_c1(ii,jj) = 100;
if(Populacao1(ii,jj)==-1)
caudal_bombado_c1(ii,jj)=200;
end
end
will never be executed, since
Populacao1(ii,jj)
cannot be identical to +1 and -1 at the same time.
.
Modified code - I'm just guessing
You should do some exercises with the debugging features in Matlab. Put some break-points in the code and run
>> [ ctc1, cbc1, ctc2, cbc2 ] = cssm();
where
function [ ctc1, cbc1, ctc2, cbc2 ] = cssm()
n_individuos = 10;
int_tempo = 6;
n_centrais = 2;
Populacao1 = randi( [ -1, 1 ], n_individuos, int_tempo * n_centrais );
[ ctc1, cbc1, ctc2, cbc2 ] ...
= cssm_( n_individuos, int_tempo, n_centrais, Populacao1 );
end
function varargout = cssm_( n_individuos, int_tempo, n_centrais, Populacao1 )
caudal_turbinado_c1 = nan( n_individuos, int_tempo*n_centrais );
caudal_bombado_c1 = nan( n_individuos, int_tempo*n_centrais );
caudal_turbinado_c2 = nan( n_individuos, int_tempo*n_centrais );
caudal_bombado_c2 = nan( n_individuos, int_tempo*n_centrais );
for ii=1:n_individuos
for jj=1:int_tempo*n_centrais
if jj <= int_tempo
if (Populacao1(ii,jj)==1)
caudal_turbinado_c1(ii,jj) = 100;
elseif(Populacao1(ii,jj)==-1)
caudal_bombado_c1(ii,jj)=200;
else
% do nothing
end
end
if jj > int_tempo && jj<= int_tempo * n_centrais
if(Populacao1(ii,jj)==1)
caudal_turbinado_c2(ii,jj-int_tempo) = 400;
elseif(Populacao1(ii,jj)==-1)
caudal_bombado_c2(ii,jj-int_tempo)=200;
else
% do nothing
end
end
end
end
varargout = { caudal_turbinado_c1, caudal_bombado_c1 ...
, caudal_turbinado_c2, caudal_bombado_c2 };
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by