How to replace the elements of a Matrix with the elements of a vector

15 visualizaciones (últimos 30 días)
Good evening, I must perform this task. I will post the code, I hope it is explicative, because it would be a hell of a matter to explain it with my own words:
close all
clear all
clc
A = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46];
delta = A(2:end)-A(1:end-1);
delta(length(delta)+1) = min(delta);
% for those wondering: delta becomes delta = [1 2 3 4 5 6 7 8 9 1]; last element is because I need a tenth element (same size as A) and so I chose the lowest one.
matrice = [0 1 0 1 1 0 0 1 1 1; 1 0 0 0 1 1 1 0 1 1];
[row, col] = find(matrice);
matrice(row, col) = delta(col);
risultato = [0 2 0 4 5 0 0 8 9 1; 1 0 0 0 5 6 7 0 9 1]; % this is what I want the code to do, just don't consider it as a part of the code
So, with this code pasted, I can explain: I want that, while scanning the rows, the matrix element becomes the element in the correspondant column of delta. You find 1 in matrice(1,2) and you replace it with delta(2), for instance.
I will need that to perform the sum along the rows.
The issue is: referring to my example, I want to say "take matrice(1,2) and replace it with delta(2)". MATLAB doesn't speak my language and doesn't read me in my mind (of course, it's a program mounted on something which is wires and binary logic, stupid me! :D), and he understands that I want something else which he can't do. He tells me:
Unable to perform assignment because the size of the left side is 12-by-12 and the size of the right side is 1-by-12.
Error in Sandbox (line 121)
matrice(row, col) = delta(col);
Why don't my good friend MATLAB and I understand each other? What can I do to come an agreement with it? :D
Thanks for your suggestion and I hope that I sound fun enough while you're reading this.
Just two things:
  • don't suggest me solutions where a for-loop is used because who's evaluating my job is very, very annoying. This stuff is going to be inside a for-loop and whenever that person sees a nested for, that person gets a mortal panic attack. I don't want dead people on my heart. Second, the code actually gets slower.
  • I'm using MATLAB r2020, but that person loves old stuff. So I need a solution compatible with r2009 (such a pain in the arse)

Respuesta aceptada

Sibi
Sibi el 1 de Dic. de 2020
Editada: Sibi el 1 de Dic. de 2020
A = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46];
delta = A(2:end)-A(1:end-1);
delta(length(delta)+1) = min(delta);
matrice = [0 1 0 1 1 0 0 1 1 1; 1 0 0 0 1 1 1 0 1 1];
output=delta.*(matrice)
  2 comentarios
Marco Boesso
Marco Boesso el 1 de Dic. de 2020
Does this work even if the number of rows of "matrice" is indetermined? It may vary, in my code.
The number of columns, of course, will remain fixed
Marco Boesso
Marco Boesso el 2 de Dic. de 2020
Thank you! I tried your code out and it works! Brilliant!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by