whats wrong with this code?

3 ビュー (過去 30 日間)
Muazma Ali
Muazma Ali 2022 年 7 月 3 日
編集済み: Image Analyst 2022 年 7 月 3 日
Hi!
Is it not possible to use logical indexing with tables..?
I have tried to do the same things with my tables as I it is supposed to be done with datasets but it is not working..
I have attached my table . I want to extract all the values of the table where vektprosent is equal to 70 and the salt is 'CaBr2'. How can this be done I am getting error message if I write something like this:
filtered=(osmotisk_data(osmotisk_data.vektprosent_best_salts==70),:)
  3 件のコメント
Image Analyst
Image Analyst 2022 年 7 月 3 日
Again, you didn't attach the table. We can't use imread() to read in an image and somehow magically convert it to the table shown in the image. You need to attach the table in a text file or a .mat file with the paperclip icon so we can read it into a table variable in MATLAB. Please attach it after reading this:

サインインしてコメントする。

回答 (2 件)

dpb
dpb 2022 年 7 月 3 日
But 'vektprosent_best_salts' is an array containing two entries per record, not one. Hence matching "70" by itself is bound to fail.
Similarly the 'Best_salts' variable is also an array. If you intended to have only two variables with them each being an array (instead of some other arrangment with a single salt and its molecular weight(?) as individual entries) then you'll have to use something like
>> m=[50 55;40 70];
>> any(ismember(m,70),2)
ans =
2×1 logical array
0
1
>>
to locate a single value.
  3 件のコメント
dpb
dpb 2022 年 7 月 3 日
ADDENDUM: Illustration of why need any and the second dimension --
Consider the m array I created that was a subset of what you posted --
m =
50 55
40 70
>> m==70
ans =
2×2 logical array
0 0
0 1
>> any(m==70)
ans =
1×2 logical array
0 1
>> m=[m;fliplr(m)]
m =
50 55
40 70
55 50
70 40
>> any(m==70)
ans =
1×2 logical array
1 1
>> any(m==70,2)
ans =
4×1 logical array
0
1
0
1
>>
Above we start with the first case -- the comparison operation returns a logical array the same size as the array (obviously) -- this can't be used as a row indexing expression in a table to pull a set of records; we want to know which record(s) contain the specific value.
The first shows that any() w/o the second argument returns a row vector -- that tells us the second column does contain what we want -- and, by pure luck, it's the second row that we want as well, so if we just used this for a case of a 2x2 array we'd not know we messed up.
Next, what happens if the array is taller than 2 records? I just duplicated the same numbers, but reversed the columns for the two additional rows.
Now, any() without the dimension second argument still returns a 2-vector but it tells as both columns contain a "70", but now its size doesn't match the height() of the table and it will fail if try to use it for that purpose.
The last shows that it is the 2nd and 4th records in the revised array that are those of interest -- and is both a column logical vector and its length matches the height() of the table (array here). That's the required logic.
Again, this just deals with the fact that you created a table with the columns as arrays -- whether that's the right way to construct the table is an entirely different question. One would presume that likely that isn't the best construction, but we simply don't have sufficient information on the application to be able to judge for sure.

サインインしてコメントする。


the cyclist
the cyclist 2022 年 7 月 3 日
In addition to the issues that @dpb mentioned, you also have simple syntax errors in your code.
My best guess as to what you intended to do is
filtered = osmotisk_data(any((osmotisk_data.vektprosent_best_salts==70)),:)
  2 件のコメント
the cyclist
the cyclist 2022 年 7 月 3 日
Re-reading your original post, and other replies, I am confused about what you actually want for the result. What is the correct output you want? Can you show us as you showed us the original table?

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by