whats wrong with this code?

1 回表示 (過去 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 件のコメント
Muazma Ali
Muazma Ali 2022 年 7 月 3 日
Here is the picture of the table:)
Image Analyst
Image Analyst 2022 年 7 月 3 日
編集済み: 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 日
編集済み: dpb 2022 年 7 月 3 日
Your Q? asked about logical addressing using the vector as the row indexing expression for a table-- I showed you how to get that logical addressing vector if the data are, as you show, in an array and not single elements. Use that expression in the addressing expression for the table just as you had before.
"any" here converts what is a logical array of two columns by height(yourtable) rows into a logical column vector that you need to address records in the table as your original posting asked for.
You could also use "==" as @the cyclist shows; I started out with ismember because of the row and to match both elements with the 'rows' optional parameter and didn't change over to match only the one value.
filtered=osmotisk_data(any(osmotisk_data.vektprosent_best_salts==70,2),:);
will also work; and "Yes, you DO need any here and you also need the optional second argument to apply it to the second dimension to get the column vector to address records in the table. The default is by column which will return for you a 2-vector row the combination of all columns in the table that contain the value, not which records contain the value.
Again, it's your table and only you know exactly what you want and what the two columns in each variable mean -- as to rearranging it to have a different data structure. We can't tell you what to do about that at least until you clearly explain what it is you have and how it is to be used.
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 件のコメント
Muazma Ali
Muazma Ali 2022 年 7 月 3 日
Are u sure that I need any here? I just want a filtered new table based on my condition..what will "any" do here?
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?

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

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by