How do I associate specific values to specific verbiage?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Brad
el 9 de Oct. de 2013
I've got 2 arrays: A = [1; 2; 3; 1; 2; 3]; and B = [ ];
I need array B to contain [Lost; Found; Unk; Lost; Found; Unk] based on the values found in A.
I'm using the find function to locate the indices of the values 1, 2, and 3. But I'm not sure what to do next.
Any ideas are appreciated. Thanks.
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 9 de Oct. de 2013
A = [1; 2; 3; 1; 2; 3]
v={'Lost'; 'Found'; 'Unk'}
B=v(A)
4 comentarios
Más respuestas (1)
Karen
el 23 de Sept. de 2024
Editada: John Kelly
el 18 de Nov. de 2024
To achieve the desired transformation of array A into array B, you can use a combination of the find function and logical indexing in MATLAB. Here’s a step-by-step solution:
matlab
Copy code
A = [1; 2; 3; 1; 2; 3];
B = strings(size(A)); % Initialize B as a string array of the same size as A
% Find indices for each value in A
index1 = find(A == 1);
index2 = find(A == 2);
index3 = find(A == 3);
% Assign the corresponding strings to the indices
B(index1) = "Lost";
B(index2) = "Found";
B(index3) = "Unk";
% Display the result
disp(B);
Here’s how it works:
- Initialization: We initialize B as a string array with the same size as A.
- Finding Indices: Using the find function, we locate the indices of elements in A that are equal to 1, 2, and 3.
- Assigning Values: We assign the corresponding string values ("Lost", "Found", "Unk") to the respective indices in B.
- Displaying Result: Finally, we display the resulting array B.
The output will be:
matlab
Copy code
6×1 string array
"Lost"
"Found"
"Unk"
"Lost"
"Found"
"Unk"
2 comentarios
Walter Roberson
el 1 de Dic. de 2024
Note that strings were not available until R2016b, and using double-quotes to represent strings was not available until R2017a -- both after the question was originally asked.
DGM
el 1 de Dic. de 2024
Editada: DGM
el 1 de Dic. de 2024
Using strings or find() isn't necessary. This also isn't acknowledging the complication of values which apparently have no group label. OP never told us how they should be labeled. Does it make sense that they have an empty label? Should they have a unique label of some sort, or should they also be part of the "unknown" group?
A = [1; 11; 3; 1; 2; 3; 55; 1; 53; 29];
labels = {'not labeled';'lost';'found';'unknown'};
% don't need to use multiple calls to find()
[islabeled,idx] = ismember(A,1:3);
% option 1: values >3 have a unique label
B = labels(idx+1);
% option 2: values >3 have an empty label
labels{1} = '';
C = labels(idx+1);
% option 3: values >3 share the 'unknown' label
idx(~islabeled) = 3;
D = labels(idx+1);
table(A,B,C,D) % purely for the presentation
Everything here should have been viable in 2013, with the possible exception of table(). Likewise, R2013b would have been the first time categorical array tools started showing up. It's plausible that OP wouldn't have had the latest version at the time.
Bear in mind that I wrote this example so that all three cases could be implemented via modifications. If you were to choose a specific goal, you can make some obvious simplifications.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!