How do I associate specific values to specific verbiage?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Brad
il 9 Ott 2013
Modificato: John Kelly
il 18 Nov 2024 alle 15:01
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 Commenti
Risposta accettata
Più risposte (1)
Karen
il 23 Set 2024
Modificato: John Kelly
il 18 Nov 2024 alle 15:01
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"
0 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!