replacing rows with values from another matrix

2 次查看(过去 30 天)
Hi,
i have 1 3625 x 1 matrix. i have used the following code to flip values based on an index. It gives me a matrix of 1185x1. (as expected)
next =1;
% figure out origin and flip the x and y axis accordingly
for i=1: length(pts)
x_values = flip(x(next:pts(i))) % this finds the walks that start at the other side of origin
next=pts(i);
end
this gives me the output i expect.
now where i am having trouble is with replacing the values in my previous matrix with these new values....
pts is 1250, 2434, 3624.
the new matrix x_values is from pts 1250 - 2434.
  2 个评论
David Hill
David Hill 2021-3-15
Not enough information to help. Showing sample of input and expected output would help. Your loop is over-riding x_values during each cycle of the loop. What does the pts() array look like?
caroline bourn
caroline bourn 2021-3-16
pts is [ 1250, 2435, 3624] this is the change in time (indicating a new test)
i know it is over-ridding. i dont want it to... i want the expected output to look like this.

请先登录,再进行评论。

采纳的回答

ANKUR KUMAR
ANKUR KUMAR 2021-3-16
Since you have not attached any speciific data, I am generating random values and indices to change.
clc
clear
original_mat=randi(20,1,100);
new_mat=randi(20,1,30);
index_to_change=45:74;
original_mat(index_to_change)=new_mat;
Hope this helps.
  3 个评论
ANKUR KUMAR
ANKUR KUMAR 2021-3-17
clc
clear
[~, ~, raw] = xlsread('expected.xlsx','Sheet1','C1:D3624');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
cellVectors = raw(:,2);
raw = raw(:,1);
data = reshape([raw{:}],size(raw));
original_mat = data(:,1);
new_mat = cellVectors(:,1);
clearvars data raw cellVectors;
replace_index=cellfun(@(x) ~isempty(x) & isnumeric(x), new_mat);
original_mat(replace_index)=cell2mat(new_mat(replace_index));
updated_matrix=original_mat;
caroline bourn
caroline bourn 2021-3-17
hi thank you for this! much appreciated. the problem is with this code i am writing it to apply to any file i get. the position of index relies on pts which in this particular case is calculated using find change pts. pts is [1250, 2435, 3624]. i also want to stay using text files.....

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by