how to vectorize a for loop in cell arrays

조회 수: 1 (최근 30일)
John
John 2020년 5월 25일
답변: Arjun 2024년 9월 10일
I have a cell array named tempfinal, which is 14x196, in which each of these entries is a 3x5 table
i have a code that turns this 14x196 cell into a 1x3 cell, in which each of these 3 cells is a 14x196 table that keeps the ith (out of 3) row of the 2nd column.
the code looks like this:
temptemp=cell(1,N);
for T=1:L
for U=1:V
for i=1:N
temptemp{1,i}(T,U)= tempfinalnew{T,U}(i,2);
end
end
end
how can i vectorize this to avoid these 3 for loops? i tried reading the arrayfun documentation but I am not sure how to get started
I would appreciate any help
  댓글 수: 2
John
John 2020년 5월 26일
part of my confusion comes from the fact that if i were to do this, for example, and get rid of one of the for-loops,
T=1:1:L;
for U=1:V
for i=1:N
temptemp{1,i}(T,U)= tempfinalnew{T,U}(i,2);
end
end
i get an error that says Expected one output from a curly brace or dot indexing expression, but there were 14 results.
Stephen23
Stephen23 2020년 5월 26일
"part of my confusion comes from the fact that if i were to do this..."
then you are generating a comma-separated list. Read these to know more:

댓글을 달려면 로그인하십시오.

답변 (1개)

Arjun
Arjun 2024년 9월 10일
Hi @John,
It seems that you are attempting to extract data from a cell array and transfer it to another cell array, aiming to minimize the use of for loops as much as possible.
This can be done using “arrayfun” function in MATLAB, which you also mentioned in your question. We can use the “arrayfun” function in MATLAB to apply a function to each element of an array without explicitly writing a loop. It also helps in simplifying and improving the readability of the code.
You can use the “arrayfun” and supply it with an anonymous function which will take two inputs(t,u) and for each combination of these two inputs it will access the ith row and 2nd column of the table stored in tempfinal(t,u) which is the original array. We also use an additional property “UniformOutput” and make it “false” so that we get a cell array as output.
Here is how we can use it in the code:
% Parameters
L = 14; % Number of rows in tempfinal
V = 196; % Number of columns in tempfinal
N = 3; % Number of rows in each table within tempfinal
% Generate sample data for tempfinal
tempfinal = cell(L, V);
% Populate tempfinal with 3x5 tables
for t = 1:L
for u = 1:V
% Create a 3x5 table with random data
data = array2table(rand(3, 5), 'VariableNames', {'Var1', 'Var2', 'Var3', 'Var4', 'Var5'});
tempfinal{t, u} = data;
end
end
% Initialize the output cell array
temptemp = cell(1, N);
% Use arrayfun to extract the ith row of the 2nd column from each table
% Main Code starts from here, till here only sample data generation was
% being done
for i = 1:N
temptemp{i} = arrayfun(@(t, u) tempfinal{t, u}{i, 2}, ...
repmat((1:L)', 1, V), ...
repmat(1:V, L, 1), ...
'UniformOutput', false);
% Convert the cell array to a matrix
temptemp{i} = cell2mat(temptemp{i});
end
% Display the result
disp('Extracted Data:');
for i = 1:N
fprintf('Data for row %d of the 2nd column:\n', i);
disp(temptemp{i});
end
The result of running a code will be “temptemp” which is a 1x3 cell array where each cell contains a 14x196 table.
Kindly refer to documentation of “arrayfun” to understand more about it.
I hope it will help!

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by