Conditionally assign values to cells based on values of preceding cells.
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm pretty new to Matlab so this might be a strange question... I have a CSV file with 48000 rows in 21 columns. In the first column I have a name followed by a bunch of numbers and then another name, more numbers, another name... and so on. What I need to do is change every block of numbers to the name that precedes it. For example, the first entry in the column is AL021985. The next 20 entries are numbers that I want to change to AL021985. The 21st entry in the column is something like AL031986 followed by N entries that I want to change to AL031986. The dataset continues like this for 48000 rows. I've imported the CSV as a cell array but haven't had much success beyond that in doing what I need to do. Any thoughts would be appreciated.
0 Commenti
Risposte (1)
Azzi Abdelmalek
il 20 Ago 2013
Example
v={'a1' 'b1' 'c1';1 2 3;4 5 6;'a2' 'b2' 'c2';8 9 2;3 5 1;'a3' 'b3' 'c3';0 1 2 ;0 0 1}
c1=v(:,1);
idx1=find(~cellfun(@isnumeric,c1))
idx2=[idx1(2:end)-1 ;numel(c1)]
R1=arrayfun(@(x) c1(idx1(x)+1:idx2(x)),1:numel(idx1),'un',0)
R1 contains all your blocks for the first column
2 Commenti
Azzi Abdelmalek
il 22 Ago 2013
[Richard commented]
Hi Azzi,
Thanks for the reply but I ended up trying something different and am stuck. I extracted the column I'm working with and imported it as a double converting the strings to unique numbers and converting the original numbers I do not need into 0. So now I have a 48,000x1 double that has a unique number followed by some number of zeros, then a unique number again, then zeros. I feel like the solution here is a conditional loop. If a block equals zero then set it equal to the first nonzero value preceding it. This way Matlab will loop through and convert the Zeros to the unique number preceding the zeros until it encounters the next unique number. Any thoughts?
Thanks
Azzi Abdelmalek
il 22 Ago 2013
Like I did above, give a short example , post your code, and show the expected result
Vedere anche
Categorie
Scopri di più su String in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!