How to simplify this for-loop into a one line of code?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Attached is a file storing a cell array A, a logical array for the columns: tf, and a logical array for A: map2.
My goal is to replace the below for-loops with a one line of code:
[m n] = size(A);
for j=1:n
for i=1:m
if ~tf(j) & map2(i,j)
A{i, j} = str2double(A{i,j});
end
end
end
Why doen't the below work?
A(~tf & map2) = str2double(A(~tf & map2));
0 comentarios
Respuesta aceptada
dpb
el 12 de Mzo. de 2025
Movida: dpb
el 12 de Mzo. de 2025
load test10
whos
A
~tf & map2
A(~tf & map2)
try
A(~tf & map2)=str2double(A(~tf & map2))
catch ME
ME.message
A(~tf & map2)=num2cell(str2double(A(~tf & map2)))
end
You are trying to stuff a double array into a cell array -- "you can't do that!" :)
Convert the numeric array to equivalent in a cell array with num2cell.
My question is still what is wrong with the input file that caused this to happen and why not fix that instead?
2 comentarios
dpb
el 12 de Mzo. de 2025
Can you not attach the file that creates the issue for somebody to poke at and see why it fails? If it is actually a flaw in readcell it could be a good tool for Mathworks; if there's something actually in the file, perhaps the generating process could be fixed instead.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!