Replace values with a string

27 次查看(过去 30 天)
Tatjana Mü
Tatjana Mü 2022-2-8
回答: Shivam 2024-1-3
Hi,
I am starting to work with Matlab. At the moment I am in the process to write a script, which should highlight outliers.
Therefore I did some calculation and currently I replaced all the outliers with a 1, and normal values with a 0. These are saved in a table (double - intense_RL). These "1s" should be replaced by the element name, which are saved in a string (element_RL). And I cant find a solution therefore.
for m=1:anzahl_elemente
intens_RL(intens_RL>SP_limit)=1; %ersetzt Ausreisser durch 1
intens_RL(intens_RL~=1)=0; %ersetzt nicht Ausreisser durch 0
end
intens_RL( ~any(intens_RL,2), : ) = []; %löscht Zeilen die nur 0 drin sind
intens_RL( :, ~any(intens_RL,1) ) = []; %löscht Spalten die nur 0 drin sind
for k=1:anzahl_elemente
intens_RL(intens_RL==1)=element_RL
end
I receive following error code:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Versuch4_SPei (line 68)
intens_RL(intens_RL==1)=element_RL
The numbers of columns are the same in intens_RL and element_RL (photos). So in the end, if there are for example 5x one in the column of Magnesium, they should be all replaced by [24Mg]+.
I appreciate all help, because I really stuck at this point. :-)
THANK YOU
  10 个评论
Stephen23
Stephen23 2022-2-8
@Tatjana Mü: or, rather than using indexing, you could then use STRREP or similar.
Voss
Voss 2022-2-8
Also, regarding the logic here:
intens_RL(intens_RL>SP_limit)=1; %ersetzt Ausreisser durch 1
intens_RL(intens_RL~=1)=0; %ersetzt nicht Ausreisser durch 0
In the first line you are saying, "if any element is an outlier, then set it to 1", and in the second line you are saying, "if any element is not 1, then set it to 0" which is not the same as what you mean to say: "if any element is not an outlier, then set it to 0". To see the difference, consider the situation where, right before those lines are executed, an element of intens_RL is equal to 1 and it's not an outlier. The first line will correctly leave it alone, but then the second line there will also leave it alone when it should be set to 0.
Maybe this situation would never arise in your data, but in general it is not what you want. It can more properly (and clearly) be written as:
is_outlier = intens_RL > SP_limit;
intens_RL(is_outlier) = 1; % ersetzt Ausreisser durch 1
intens_RL(~is_outlier) = 0; % ersetzt nicht Ausreisser durch 0

请先登录,再进行评论。

回答(1 个)

Shivam
Shivam 2024-1-3
Hello Tatjana,
Based on the details you've shared, it appears that you are encountering an error when trying to assign element names to an intensity array that contains values of 1.
After reviewing and debugging the provided code and converting the intens_RL array to a string type, you can see that 'intens_RL' is a 3x4 string array and 'element_RL' is a 1x5 string array, as shown below:
>> intens_RL
"0" "0" "1" "1"
"0" "1" "0" "0"
"1" "0" "0" "0"
>> element_RL
"[23Na]+" "[24Mg]+" "[189Os]+" "[209Bi]+" "UO+"
And to perform the following assignment operation, i.e.
intens_RL(intens_RL == "1") = element_RL
the number of elements in the 'intens_RL' matrix with the value "1" must match the number of elements in the 'element_RL' array. However, 'intens_RL' contains four "1"s, and 'element_RL' consists of five strings.
You can reconsider the calculations performed to calculate the 'intens_RL' array to deal with the size mismatching.
I hope it helps in resolving the error.
Thanks

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by