0×1 empty double column vector

조회 수: 14 (최근 30일)
Patrick Hartzsch
Patrick Hartzsch 2020년 2월 12일
댓글: Shubhanshi Mishra 2021년 7월 3일
Hi,
I have an array of 19x2 double (r_F), contains 19 integers in column 1 and 2. Now I want to do a vlookup of integers that are stored in the array auswahl; the size is 100x1. If the integer of auswahl were find in column 1 of r_F then write the integer of column 2 in the array R_X. The following code do that for me.
R_X = [];
for i = 1:size(auswahl, 1)
index = find(r_F(:, 1) == auswahl(i));
content = r_F(index, 2);
disp(i + ": " + index + " " + content)
R_X = [R_X; content];
end
Strangely enough contains the result on index 23 and 74 0x1 empty double column vector?! Has anyone an idea why this is happen?
Kind regards,
Patrick
  댓글 수: 2
Guillaume
Guillaume 2020년 2월 12일
Comment by Patrick Hartzsch originally posted as an answer moved here:
Supplement: I fill the vector auswahl with the following command:
auswahl = round(rand(100,1)*19,0);
It seems, that auswahl contains not only integers.
Shubhanshi Mishra
Shubhanshi Mishra 2021년 7월 3일
Hello;
I have a matrix(100*4). I want to get the index of a particular value in 4th column. I am using..
idx = find(matrix(:,4)==desiredvalue)
for the values of indices 2 to 50, it is coming as 0*1 empty double column vector and for indices 51 to 100, it is coming correct.
I am not able to understand , why it is so.
please help.

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

답변 (1개)

Guillaume
Guillaume 2020년 2월 12일
a) If you want to generate integers between 0 and 19 use:
auswahl = randi([0 19], 100, 1); %use randi to generate random integers
b) Despite what you may have been taught, loops are rarely needed in matlab and often complicates everything. To keep the elements in the second column of r_F for which the first column is found in auswahl:
R_X = r_F(ismember(r_F(:, 1), auswahl), 2); %keep elements of column 2 of r_F for which column 1 is a member of auswahl
  댓글 수: 8
Guillaume
Guillaume 2020년 2월 14일
편집: Guillaume 2020년 2월 14일
I'm afraid I can't see anything wrong with the results produced by your script. Both auswahl and the first column of r_F are guaranteed to contain integers due to the way you construct them.
edit: However note that auswahl contains integers from 0 to 19 included, while r_F contains integers from 1 to 19. Perhaps, you meant to have integers from 1 to 19 in auswahl all along, in which case:
  • I did write "If you want to generate integers between 0 and 19 use"
  • That's what your original round(rand(100,1)*19,0) did
If you want integers between 1 and 19 (included) then:
auswahl = randi(19, n, 1)
See the documentation of randi.
Patrick Hartzsch
Patrick Hartzsch 2020년 2월 15일
Yes, that's was the problem. Your last code solve the problem!
Many thanks for your support and patience

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by