How to loop column data
1 view (last 30 days)
Show older comments
Hi,
I have a 200*100 table data. Columns in the table are named as 'Vre1', 'Vre2',...'Vre100'. The table has 200 rows.
I want to loop the table each time use one column for my following calculation.
The first round in the loop:
AnotherData.column1 = min(AnotherData.column1, LoopTable.Vre1);
AnotherData.column2 = min(AnotherData.column2, LoopTable.Vre1);
The second round in the loop would be:
AnotherData.column1 = min(AnotherData.column1, LoopTable.Vre2);
AnotherData.column2 = min(AnotherData.column2, LoopTable.Vre2);
The last round in the loop would be:
AnotherData.column1 = min(AnotherData.column1, LoopTable.Vre100);
AnotherData.column2 = min(AnotherData.column2, LoopTable.Vre100);
And after each loop, my AnotherData should be renamed as AnotherDara1, AnotherDara2, ...AnotherDara100.
Finally, I am should calculate the statistical median of AnotherData.
Please tell me how cna I do that. Thank you.
AnotherData=rand(10,2);
AnotherData=array2table(AnotherData);
LoopTable=rand(10);
for ii=1:10;
X=LoopTable(:,ii); % there's aproblem in this line
AnotherData.AnotherData1 = min(AnotherData.AnotherData1, X);
AnotherData.AnotherData2 = min(AnotherData.AnotherData2, X);
AnotherData{ii} = AnotherData; % how to improve this line to get a statistical median of AnotherData.
end
5 Comments
Bob Thompson
on 15 Jan 2019
"ConResult(i)=result; % this line seems not right.How can I concatenate my array columns together?'
You are correct that this won't work. You should be looking at something more like this:
ConResults = reshape(Results,[],1);
This will make ConResults a single column of all the data in 'Result'.
I'm not sure why you are indexing ConResult, but I assume it is to store the data for each loop. The way you have it set up now won't work. Currently you are trying to fit an entire array into a single element. This only works if the element you are trying to place the array in is a cell, which requires curly braces to define. If you are looking to add a new column to ConResults, then your indexing should be something more like (:,i).
Answers (1)
Guillaume
on 15 Jan 2019
You keep mentioning loops when no loop is ever needed. I'm going to assume I was correct as to the result you want. this can be achieved simply with:
%inputs:
%A: a MxN array (if it was a table convert it to a matrix with yourtable{:, :})
%B: a MxP array
%output: C a MxNxP array where C(:,n, p) = min(A(:, n), B(:, p))
result = min(A, permute(B, [1 3 2])); %That's it! (At least in versions >= R2016b)
After that I'm not sure what you want to calculate. It looks like you want the median, but I'm not sure of what.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!