0×1 empty double column vector

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 Kommentare

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 am 3 Jul. 2021
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Guillaume
Guillaume am 12 Feb. 2020

0 Stimmen

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 Kommentare

a) thank you
b) R_X must have the same size like auswahl, in my case 100x1. But this command
R_X = r_F(ismember(r_F(:, 1), auswahl), 2);
gives as result an 19x1 array.
My goal is to search each element from auswahl in r_F an write the result in R_X (like the VLOOKUP (SVERWEIS in german) function in EXCEL).
Ah, ok. then it's:
[found, where] = ismember(auswahl, r_F(:, 1)); %find which elements of auswahl are in first column of r_F and in which row
R_X = r_F(where(found), 2); %get matching second column of r_F
Patrick Hartzsch
Patrick Hartzsch am 13 Feb. 2020
Ok, the variables found and where were 100x1 logical and 100x1 double. But the result R_X is an 86x1 double. In addition to 1, the array R_X also contains 0. I suspect that the integer values in auswahl and r_F sometimes do not match?!
Okay,
when I create the vector
auswahl = randi([0 19], m, 1);
the behavior described above occurs. If I create the vector
auswahl
manually and paste the integers in it, the code were running fine and auswahl and R_X have the same size.
"Ok, the variables found and where were 100x1 logical and 100x1 double. But the result R_X is an 86x1 double"
Nothing surprising about that. Indeed found and where should be the same size as auswahl. R_X is going to have as many elements as there are true (1) values in found, so it can be 86x1 if nnz(found) is 86.
"when I create the vector [with randi] the behavior described above occurs. If I create the vector manually and paste the integers in it, the code were running fine.
randi creates integers. I suspect that the values in r_F (the first column) are not exact integers (maybe of by 2e-16 from the integer value) and what you copy manually are also not exact integers. Attach a mat file containing r_F so we can check.
or test it yourself with:
rows = find(mod(r_F(:, 1), 1) ~= 0);
assert(isempty(rows), 'row(s) %s of first column of r_F contain non-integers', strjoin(compose('%d', rows), ', '))
Please find attached
test.m
Thank you for your help
Guillaume
Guillaume am 14 Feb. 2020
Bearbeitet: Guillaume am 14 Feb. 2020
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 am 15 Feb. 2020
Yes, that's was the problem. Your last code solve the problem!
Many thanks for your support and patience

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2019b

Gefragt:

am 12 Feb. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by