How to solve "Subscripted assignment dimension mismatch." error??

조회 수: 6 (최근 30일)
Luís Quesado
Luís Quesado 2017년 12월 30일
댓글: Luís Quesado 2018년 1월 2일
Hi. I'm trying to write a piece of code with the follow objective: A cicle "for" that creates an array x with three binary numbers, convertes the binary to decimal number, and then saves the binary of each iteration in an diferent array called genes.
21: genes = [row, 3]
22: for r = 1:row
23: x = round(rand([1,3])) %Creates array with binary number
24: d = bi2de(x) %Convert binary to decimal
25: genes(r,:) = x %Saves binary number of each iteration
The error "Subscripted assignment dimension mismatch." keeps apearing on line 25. I've been trying to correct this for hours, i can't understand what i'm doing wrong. I have already tried to change the array genes on line 25 to genes(r, 3) but still returns an error. Sorry for my English it's not my first language

채택된 답변

Matt J
Matt J 2017년 12월 31일
This should be
21: genes = nan([row, 3]);

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 30일
Why do you convert to decimal but then save the binary?
Your x is going to be a row vector of length 3; you would have difficulty if your genes array is is initialized but does not have 3 columns.
  댓글 수: 1
Luís Quesado
Luís Quesado 2018년 1월 2일
I'm trying to create an genetic algorithm. So the decimal corresponds to a certain decision, and the "genes" variable are the genes of that "decision". I'm saving the binary so i can do cross-over and genetic mutation with it later.

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

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by