필터 지우기
필터 지우기

Creating matrix from a for loop

조회 수: 2 (최근 30일)
athpapa -
athpapa - 2011년 11월 13일
I have this 'for loop' (where A a matrix from my program):
for m=symbols(1:1000)
d1=abs(m-A);
[C,I]=min(d1);
k=A(I);
end
I want from this loop to save the values of 'k' (1000 total) to a (1, 1000) matrix? How can I do this because I am getting some errors!
Thank you...

채택된 답변

Sven
Sven 2011년 11월 13일
Hi athpapa,
I don't know what your "A" matrix or "symbols" matrix had, so I will just make them random numbers:
A = randi(500, 10) - 250;
symbols = randi(30,1,1000);
Now, we can make "k" as a vector of numbers (rather than just a single number). Note that I use "i" to iterate from 1:1000 so that we know which element of "k" to save our answer to:
k = zeros(1,1000);
for i = 1:1000
m = symbols(i);
d1=abs(m-A);
[C,I]=min(d1(:));
k(i) = A(I);
end
Does this do what you wanted it to do?
  댓글 수: 3
Sven
Sven 2011년 11월 13일
The issue here is *all* about how to store "B", because the way you have it:
B=[00 01 11 10]
will not actually store the "binary bit" aspect - it will just convert it to numbers:
B=[0 1 11 10]
You can try perhaps defining "B" as a cell array of strings:
B={'00' '01' '11' '10'}
In which case I think it will work how you intended:
B([1 3 2 4 1 3])
If this doesn't quite answer you question, it's a good idea to make a new question and post it (since this is off-topic from the original)
Walter Roberson
Walter Roberson 2011년 11월 13일
dec2bin(A,2) - '0'

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

추가 답변 (0개)

카테고리

Help CenterFile 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